-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Divide and conquer algorithms", exercise 1 #26
Comments
I let a line I used to debug the procedure.
|
This would be my implementation:
However, I am not quite sure whether the function should also work with start and end values other than the actual start and end of the list (i.e. more than 0 and less than 8 for |
It took me quite some time but I think I got to a working solution. The test case I used runs the function for each letter of the English alphabet on a list only containing the Italian one. The letters not in the alphabet return "not found" as expected.
|
I have a bunch of test cases because as I worked through it I had a bunch of weird problems I didn't understand (including it not working with odd-numbered When I got the recursion error in Python Tutor I saw in the step-by-step explanation they give you that I got the error when my The one problem I couldn't solve was for it handle empty lists. At one point it was able to return "None" for empty sets but that version of the code couldn't handle looking for things in the first half of an odd-numbered list. I don't know why. If anyone does, let me know. In its current state it works for everything (or so I think) except an empty list, which will give an index error and make it stop running. You'll see I've left my example of that as a comment in the code. If anyone knows how to make it so it can handle an empty list, please let me know. Also is an empty list an unordered list? If you're having problems and don't know why, doublecheck that your list is actually in order.
|
|
|
|
|
|
|
|
"""
""" |
I've not completed the implementation with the test, 'cause the code (that I suppose it's correct) gives me a wrong solution in the second print case. I can't come up with the error in this code. Could you help me, please?
|
|
|
|
|
|
Hi guys, a few comments about your solutions as follows. About @AleRosae's comment:
Well, in the recursive step you already do that, right? If you have a About @SarahTew's comment:
You have to check explicitly if the input list is empty or not before continuing, and then return About @giorgiasampo, @ChiaraCati, @laurentfintoni, @GiuliaMenna, @gabrielefiorenza, @LuisAmmi, @SofiBar and @AlessandraFa's code:
Doing that is kind of cheating since you are checking if the item is in the list before looking for it. But the binary search should not know that in advance: it should return |
Implement in Python the binary search algorithm – i.e. the recursive function
def binary_search(item, ordered_list, start, end)
. It takes an item to search (i.e.item
), an ordered list and a starting and ending positions in the list as input. It returns the position ofitem
in the list if it is in it, andNone
otherwise. The binary search first checks if the middle item of the list betweenstart
andend
(included) is equal toitem
, and returns its position in this case. Otherwise, if the middle item is less thanitem
, the algorithm continues the search in the part of the list that follows the middle item. Instead, in case the middle item is greater than item, the algorithm continues the search in the part of the list that precedes the middle item. Accompany the implementation of the function with the appropriate test cases. As supporting material, Fekete and Morr released a nonverbal definition of the algorithm which is useful to understand the rationale of the binary search steps.The text was updated successfully, but these errors were encountered: