-
Notifications
You must be signed in to change notification settings - Fork 8
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 algorithm", exercise 1 #25
Comments
This was took quite a long while. There is probably a cleaner, shorter way to solve this. Here is one that, at least, works for many different inputs: Clean code:
Code with comments
|
Here is another way.. Clean code
Code with comments
|
[] |
@Hizkie and I found a solution after 2 days of working. Our solution works for all probable pivot_positions.
|
I've understood in the end how Delfina & Lisa sorted the problem.
True
|
Here is what I meant on my voice note:
Thought about it more and shortened it just a bit:
|
The following algorithm employs the principles of "divide and conquer algorithms" (base case, divide, conquer, combine). In order to implement the divide and conquer approach, the algorithm uses two inner functions of which one is using itself recursively. When I am doing the second exercise tomorrow, I will try to improve this algorithm. If I am able to tweak the code I'll post an update.
|
Simple, clean approach based on Prof. Peroni's explanation in class today.
|
I'm extremely happy to be posting the solution of this exercise after so many hours over it, nightmares (for real) and hints from colleagues :D
test_partition(['oranges', 'banana', 'strawberry', 'pineapple', 'apple', 'pear'], 1, 1, 4, "oranges") #true |
Here I propose two possible solutions: the first algorithm uses a sublist and it works; however, I wanted to use the range and I have been struggling for hours trying to find a solution. So the second code I propose uses the range, but it worked only with some tests so I know it is not correct, but maybe it can be a start, of course only in the case in which we can actually use range for solving this exercise. Suggestions are more than welcome!
|
|
Hi guys, That has been the most interesting discussion we had so far in the entire course. I really thank you for all you effort in solving it, providing an incredible number of different perspectives and angles to look at this problem. It has been great. Here my solution to the problem - I hope you can find it clear but I'm happy to discuss it in the next lecture. The source code is available online as usual.
|
Implement in Python the partition algorithm – i.e.
def partition(input_list, start, end, pivot_position)
– that takes a list and the positions of the first and last elements in the list to consider as inputs, and redistributes all the elements of a list having position included betweenstart
andend
on the right of the pivot valueinput_list[pivot_position]
if they are greater than it, and on its left otherwise. In addition, the new position where the pivot value is now stored will be returned by the algorithm. For instance, considering my_list = list(["The Graveyard Book", "Coraline", "Neverwhere", "Good Omens", "American Gods"])
, the execution ofpartition(my_list, 1, 4, 1)
changesmy_list
as follows:list(["The Graveyard Book", "American Gods", "Coraline", "Neverwhere", "Good Omens"])
and 2 will be returned (i.e. the new position of"Coraline"
). Note that"The Graveyard Book"
has not changed its position in the previous execution since it was not included between the specified start and end positions (i.e. 1 and 4 respectively).Accompany the implementation of the function with the appropriate test cases.
The text was updated successfully, but these errors were encountered: