Lists: Difficult Solutions
List functions
Draw out a box and pointer diagram for lst1 and lst2.
lst1 = [1, 2, 3, 4]
lst2 = [5, 6, 7, 8]
lst1[lst2[0] - lst1[1]] = lst2
lst2.append(lst1.remove(2))
lst1.extend([lst2[len(lst1)]])
lst2.append([lst1[len(lst1) - lst1[2][0] + lst1[lst1[0]]]])
code writing
Write a function that will take in a list and sort it using quicksort. Quicksort takes the first element and partitions the list into three partitions: elements less than the first element, elements equal to the first element, and elements greater than the first element. Then it recursively applies those steps to the partitions.
def quicksort(lst):
>>> lst = [4, 6, 7, 2, 1, 3]
>>> quicksort(lst)
[1, 2, 3, 4, 6, 7]
if lst == []:
return []
less = [x for x where x < lst[0]]
equal = [x for x where x == lst[0]]
greater = [x for x where x > lst[0]]
less = quicksort(less)
greater = quicksort(greater)
return less + equal + greater