-
Notifications
You must be signed in to change notification settings - Fork 0
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 2 #29
Labels
exercise
Exercise
Comments
|
def test_partition(input_list, start, end, pivot_position, expected):
result = partition(input_list, start, end, pivot_position)
if expected == result:
return True
else:
return False
def partition(input_list, start, end, pivot_position):
if start > pivot_position or pivot_position > end:
return None
j = start
i = start - 1
pivot_item = input_list[pivot_position]
while j <= end:
if input_list[j] >= pivot_item:
j += 1
else:
input_list[j] < pivot_item
i += 1
input_list[j] , input_list[i] = input_list[i], input_list[j] # 入れかえる
j += 1
input_list.remove(pivot_item)
input_list.insert(i+1, pivot_item)
return input_list.index(pivot_item)
my_list1= list(["The Graveyard Book", "Coraline", "Neverwhere", "Good Omens", "American Gods"])
my_list2 = [0,8,0,6,5,7,3,3,2,1,3]
my_list3 = ["Harry", "Ron", "Hermione", "Dumbledore", "Snape", "Voldemort"]
print(test_partition(my_list1,1,4,1,2)) #True
print(test_partition(my_list2,0,len(my_list2)-1,3,8)) #True
print(test_partition(my_list3,0,len(my_list3)-1,3,0)) #True
print(test_partition(my_list3,0,3,4,None)) #True |
![]() I took a slightly different approach, moving the pivot position[index] to the end of the list and then comparing (using j) each value in the list. I hope it is a nice alternative.... I used one of the lists used by @ValkyrieCain9 to see if I got the same result. Seemed to work... |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implement in Python the partition algorithm – i.e. the non-recursive function
def partition(input_list, start, end, pivot_position)
. It takes a list and the positions of the first and last elements to consider as inputs. It redistributes all the elements of a list having position included between start and end on the right of the pivot valueinput_list[pivot_position]
if they are greater than it, and on its left otherwise – note:pivot_position
must be a value betweenstart
andend
(included). Also, the algorithm returns the new position where the pivot value is now stored. For instance, considering my_list = list(["The Graveyard Book", "Coraline", "Neverwhere", "Good Omens", "American Gods"])
, the execution ofpartition(my_list, 1, 4, 1)
changes my_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. As supporting material, Ang recorded a video that is useful to understand the rationale of the partition steps.The text was updated successfully, but these errors were encountered: