I need to write a function that takes a list and bisects it (like the bisect module but I cannot use that). I normally would show what I have done so far but I really do not know how to do it without the module so I am hoping someone can help me out a bit. Here is the exact question I need to figure out:
Write a function called bisect that takes a sorted list and a target value and returns the index of the value in the list, if it’s there, or None if it’s not
The bisect module keeps track of a list, keeping it sorted, without having to resort every time you insert an element. The method you need to implement just needs to search inside a sorted list.
def bisect(sortedlist,targetvalue,firstindex=0,lastindex=None):
if(len(sortedlist)==0):
return None
if(len(sortedlist)==1):
if(sortedlist[0]==targetvalue):
return firstindex
else:
return None
center = int(round(len(sortedlist)/2))
if(sortedlist[center]==targetvalue):
return firstindex+center
if(targetvalue>sortedlist[center]):
return bisect(sortedlist[center+1:lastindex],targetvalue,center+1,lastindex)
else:
return bisect(sortedlist[0:center],targetvalue,firstindex,center-1)
this basically does a binary search.
The indexes are passed to keep track of the indexes of the original list, in invocations further down the recursion loop.
i was doing the homework from "think python" chapter 10 exercise 8 and i tired the code above written by fred. it seems to have some bugs.
1. the counter doesn't work for long list with 100k strings
2. sometimes it returns None for things that i'm sure is in the list.
so i tweaked it a little bit:
this is my version:
it works very well, it tested it with a wordlist from swampy 2.0 named words.txt, which originally comes from the moby collection: 113809of.fic
hope this helps those of you struggling with the bisect program
def bisects (list,x,counter=0):
if len(list)==0:
return x,'is not in the list'
elif(len(list)==1):
middle = 0
else:
middle = int(len(list)/2)
if x == list[middle]:
return counter, x,'is in the list'
elif x < list[middle]:
counter +=0
return bisects(list[0:middle],x,counter)
elif x > list[middle]:
counter +=middle
return bisects(list[middle+1:],x,counter)
Also will be great if a guru can help me correct that flaw, thanks,
Related
I feel confused about the code as follows:
define this findmin function to find the smallest number in alist, O(n^2)
def findMin(alist):
overallmin=alist[0]
for i in alist:
issmallest=True
for j in alist:
if i>j:
issmallest=False
if issmallest:
overallmin = i
return overallmin
I can't understand: why the author set "issmallest = True" below the first for loop? Commonly when we feel like to use boolean values like this situation(such as assign boolean value to a variable at the beginning of the code or put the boolean values as the if-statement conditions)? Thanks!
Okay so first off, to erase a few of the mistakes in the code, a working example would be:
def findMin(alist):
overallmin=alist[0]
for i in alist:
issmallest=True
for j in alist:
if i>j:
issmallest=False
if issmallest:
overallmin = i
return overallmin
The idea behind this code is to compare all elements in a list with all others and keep the one that is smaller than all others. In the loop it is therefore assumed that the current element is the smallest issmallest = True until a smaller one is found. If a smaller one is found, the value of issmallest is changed to False. So if, after comparison with all others, issmallest is still True, then the element is truly the smallest and therefore fixed as such.
You could simplify this code , as there is no need in further comparing once the smallest element is found, i.e. you can leave the function. Also in this algorithm there is no need of keeping a variable for the smallest element. The corresponding code might read something like:
def findMin(alist):
for current_el in alist:
issmallest = True
for other_el in alist:
if other_el < current_el:
issmallest = False
if issmallest:
return current_el
But: Even for beginners, this is not a good code to find the minimum. I can say that as a beginner myself. It is much cleaner to go through the list once, with an element at hand, compare it while going through, and always keep the smallest.
So even with low afford you can write a much faster algorithm like this:
def findMin(alist):
smallest_el = alist.pop() # take out an element, no need to compare with itself
for other_el in alist:
if other_el < smallest_el:
smallest_el = other_el
return smallest_el
The condition of while always must evaluate to boolean. if it evaluates to true, the loop continues, otherwise, the statements after the loop will be executed. Please note: the code in your question did not involve a while loop. This code is very bad, please forget it, and everything that came with it. For learning, try this: https://www.learnpython.org/.
I'm trying to do a bingo game, I had some struggle with it but finally sorted it out.
However, my main "problem" (more like, I've heard its bad programming) is that with my function I'm calling my function inside it in an else statement. I don't think that it's how you suppose to do it, i have not found any way around it though.. Because this function is called from another function called menu() so when i use a loop, it goes back to the menu if false.
Here's my code:
def selectingNumbers():
numbers = []
dupl = []
j = 0
print("Now you are gonna select 5 number\n")
while j < 5:
nummer = int(input("Your choice:\n"))
numbers.append(int(nummer))
j = j+1
for i in numbers:
if i not in dupl:
dupl.append(i) #New list without duplicates
if dupl == numbers: #Comparing new list with old list
print("No equal numbers found")
dragning(numbers)
else:
print("Equal numbers found")
selectingNumbers() #Is there a better way to do it?
I also had some issues with the list at the beginning, I know I can use the set() function but i want to keep the original list as it is and compare the new one with the old one, can I do that in a better way with "real" programming instead of import modules?
Hope you can answer or guide me on these two questions with alternatives and if so, say why my code is "bad" if it is.
Well you have to decide if you want to use recursion to solve the problem. This line is a recursive call:
selectingNumbers() #Is there a better way to do it?
Which is fine, and does not equate to bad programming. However, the rest of your function does no cater to a recursive function. You reset your variables and have no true base case because of that. See google, or here for examples.
Recursion is confusing for beginners, so I would take an iterative only approach. Here is a bingo python example.
In addition, I'm not sure this line works:
if dupl == numbers: #Comparing new list with old list
I am not too familiar with python, but in my experience, arrays are treated as objects, so in that line you would be asking python to compare two seperate objects with unique references in memory. So they will never be equal, even if the values inside of them are the same because they are both referenced seperately. I found this link to answer that concern.
Recursion isn't "bad". In fact it can sometimes greatly simplify a solution to a problem. However in the case of your code it isn't necessary. Fortunately, it can sometimes be replaced with a loop. In the case of your code it looks like it could just loop until it gets a list from the user that doesn't contain any duplicates. That means it could be rewritten as shown below (I also simplified a few other things):
def selectingNumbers():
while True:
print("Now you are gonna select 5 different numbers\n")
numbers = []
for _ in range(5):
number = int(input("Your choice:\n"))
numbers.append(number)
unique = set(numbers) # will remove any duplicates
if len(unique) == len(numbers): # no dups?
print("No equal numbers found")
break # <--- terminates loop
else:
print("Equal numbers found")
# allow loop to continue
dragning(numbers)
I have lists which consist of random letters..
def similarity(out_list):
i=0
count=0
while i<len(out_list):
if out_list[i][-1]==out_list[i+1][-1]:
count+=1
return similarity(out_list[i][:-1]) + count
elif out_list[i][-1]!=out_list[i+1][-1]:
return similarity(out_list[i][:-1])
i+=2
out_list=["ABABA","ACA","AGAGA","AAVA","XBX","ARAA","AADA","AAA","BABAB","ABA"]
similarity(out_list)
In my code I'm trying to find difference between list elements which are first and second, third and forth etc..
However since my function is recursive, the value of i is always 0 and I can't control other elements and I can't find the difference..
for ABABA and ACA the difference is 3 because first A and third A are the common in both words at same indexes.. so difference 5-2=3
what changes do my code require? Thank you.
Consider passing i as a parameter into your recursive function instead of setting it as 0 at the very start of the function. It's likely you would want to do something similar to count too.
I was wondering if someone can help me to fix the error my code for quick sort has:
It does not compile and highlights the last line of the code in red.
I can not figure out what is wrong. sort is already defined as a function so why is it highlighted as red?
def sort(*myarray):
less = []
equal = []
greater = []
if len(myarray) > 1:
pivot = myarray[0]
for x in myarray:
if x < pivot:
less.append(x)
if x == pivot:
equal.append(x)
if x > pivot:
greater.append(x)
return sort(less)+sort(equal)+sort(greater)
else:
return myarray
print sort([12,4,5,6,7,3,1,15])
You're defining the function as taking a variable number of arguments (the *myarray bit), but then using myarray inside as a single argument (the list to sort), when it is a list containing the list to sort.
You probably should remove the * from your function parameter. This questions esplains it quite thoroughly.
You could keep the *, but then you would have to play a bit with tuple unpacking to get the same result.
edit
Although the above is true, this might not be the issue you're encountering.
IDLE will give you the invalid syntax error on the ast line, because in interactive mode - with lines starting with >>>, it accepts only one statement at a time. In your case that statement is the sort() definition.
Try hitting enter 2 times after the function definition, this should get you back to the repl, where you can introduce another statement (print sort([12,4,5,6,7,3,1,15]))
There are a couple things wrong which makes me curious how you are testing this:
Python code is not "compiled", it is interpreted. (Okay, not precisely true; it's parsed into a sort of byte-code; still, it's not compiled in the same sense as a language such as C, where the entire program has to be converted into machine instructions before any of it can be run.) Also you mention the last line of code is highlighted in red -- by what?
This code actually works, but only if you remote the star/asterisk in front of myarray in def sort(*myarray):. Otherwise it actually returns a single-element tuple containing the original array.
Assuming you have two or more elements that equal a pivot at some point, you get an infinite loop, because you will get: equal = [x,x] (two elements at least), and then invoke sort([x,x]), which in its turn will take x as a pivot, and create equal = [x,x], and cause sort([x,x]), ....
Simple solution to this problem: What should be the output of the sort(equal)? How do you sort a list of identical elements?
Edit: Well, your comments show that you are looking for a different problem, but I'll leave it here because it explains a different issue you have with your code and should be solved.
If it is a function for quick sorting, can you really use the function sort in it?
Wouldn't something like this work?
def qsort(list):
pivind=0
left, right, pivot= [], [], []
for x in list:
if list[pivind]==x: pivot.append(x)
elif list[pivind]>x: left.append(x)
else: right.append(x)
if len(left)>1: left=qsort(left)
if len(right)>1: right=qsort(right)
return (left + pivot + right)
I am just learning programming in Python for fun. I was writing a palindrome program and I thought of how I can make further improvements to it.
First thing that came to my mind is to prevent the program from having to go through the entire word both ways since we are just checking for a palindrome. Then I realized that the loop can be broken as soon as the first and the last character doesn't match.
I then implemented them in a class so I can just call a word and get back true or false.
This is how the program stands as of now:
class my_str(str):
def is_palindrome(self):
a_string = self.lower()
length = len(self)
for i in range(length/2):
if a_string[i] != a_string[-(i+1)]:
return False
return True
this = my_str(raw_input("Enter a string: "))
print this.is_palindrome()
Are there any other improvements that I can make to make it more efficient?
I think the best way to improvise write a palindrome checking function in Python is as follows:
def is_palindrome(s):
return s == s[::-1]
(Add the lower() call as required.)
What about something way simpler? Why are you creating a class instead of a simple method?
>>> is_palindrome = lambda x: x.lower() == x.lower()[::-1]
>>> is_palindrome("ciao")
False
>>> is_palindrome("otto")
True
While other answers have been given talking about how best to approach the palindrome problem in Python, Let's look at what you are doing.
You are looping through the string by using indices. While this works, it's not very Pythonic. Python for loops are designed to loop over the objects you want, not simply over numbers, as in other languages. This allows you to cut out a layer of indirection and make your code clearer and simpler.
So how can we do this in your case? Well, what you want to do is loop over the characters in one direction, and in the other direction - at the same time. We can do this nicely using the zip() and reversed() builtins. reversed() allows us to get the characters in the reversed direction, while zip() allows us to iterate over two iterators at once.
>>> a_string = "something"
>>> for first, second in zip(a_string, reversed(a_string)):
... print(first, second)
...
s g
o n
m i
e h
t t
h e
i m
n o
g s
This is the better way to loop over the characters in both directions at once. Naturally this isn't the most effective way of solving this problem, but it's a good example of how you should approach things differently in Python.
Building on Lattyware's answer - by using the Python builtins appropriately, you can avoid doing things like a_string[-(i+1)], which takes a second to understand - and, when writing more complex things than palindromes, is prone to off-by-one errors.
The trick is to tell Python what you mean to do, rather than how to achieve it - so, the most obvious way, per another answer, is to do one of the following:
s == s[::-1]
list(s) == list(reversed(s))
s == ''.join(reversed(s))
Or various other similar things. All of them say: "is the string equal to the string backwards?".
If for some reason you really do need the optimisation that you know you've got a palindrome once you're halfway (you usually shouldn't, but maybe you're dealing with extremely long strings), you can still do better than index arithmetic. You can start with:
halfway = len(s) // 2
(where // forces the result to an integer, even in Py3 or if you've done from __future__ import division). This leads to:
s[:halfway] == ''.join(reversed(s[halfway:]))
This will work for all even-length s, but fail for odd length s because the RHS will be one element longer. But, you don't care about the last character in it, since it is the middle character of the string - which doesn't affect its palindromeness. If you zip the two together, it will stop after the end of the short one - and you can compare a character at a time like in your original loop:
for f,b in zip(s[:half], reversed(s[half:])):
if f != b:
return False
return True
And you don't even need ''.join or list or such. But you can still do better - this kind of loop is so idiomatic that Python has a builtin function called all just to do it for you:
all(f == b for f,b in zip(s[:half], reversed(s[half:])))
Says 'all the characters in the front half of the list are the same as the ones in the back half of the list written backwards'.
One improvement I can see would be to use xrange instead of range.
It probably isn't a faster implementation but you could use a recursive test. Since you're learning, this construction is very useful in many situation :
def is_palindrome(word):
if len(word) < 2:
return True
if word[0] != word[-1]:
return False
return is_palindrome(word[1:-1])
Since this is a rather simple (light) function this construction might not be the fastest because of the overhead of calling the function multiple times, but in other cases where the computation are more intensive it can be a very effective construction.
Just my two cents.