Order of growth
Questions
What is the order of growth for the following functions?
1 | def sum_of_factorial(n): |
The order of growth is $\Theta(n^2)$
1 | def bonk(n): |
The order of growth is $\Theta(log(n))$
1 | def mod_7(n): |
This order of growth is $\Theta(1)$
Linked List
Questions
Write a function that takes in a Python list of linked lists and multiplies them
element-wise. It should return a new linked list.
If not all of the Link objects are of equal length, return a linked list whose length is
that of the shortest linked list given. You may assume the Link objects are shallow
linked lists, and that lst of lnks contains at least one linked list.
1 | def multiply_lnks(lst_of_lnks): |
For our base case, if we detect that any of the lists in the list of Links is empty, we can return the empty linked list as we’re not going to multiply anything.
Otherwise, we compute the product of all the firsts in our list of Links. Then, the subproblem we use here is the rest of all the linked lists in our list of Links.
Remember that the result of calling multiply_lnks
will be a linked list! We’ll use the product we’ve built so far as the first item in the returned Link, and then the result of the recursive call as the rest of that Link.
Iterative solution:
1 | def multiply_lnks(lst_of_lnks): |
The iterative solution is a bit more involved than the recursive solution. Instead of building the list “backwards” as in the recursive solution (because of the order that the recursive calls result in, the last item in our list will be finished first), we’ll build the resulting linked list as we go along.
We use head and tail to track the front and end of the new linked list we’re creating.
Our stopping condition for the loop is if any of the Links in our list of Links runs out of items.
Finally, there’s some special handling for the first item. We need to update both head and tail in that case. Otherwise, we just append to the end of our list using tail, and update tail
Write a function that takes a sorted linked list of integers and mutates it so that
all duplicates are removed.
1 | def remove_duplicates(lnk): |
For a list of one or no items, there are no duplicates to remove.
Now consider two possible cases:
- If there is a duplicate of the first item, we will find that the first and second items in the list will have the same values (that is,
lnk.first == lnk.rest.first
).
We can confidently state this because we were told that the input linked list is in sorted order, so duplicates are adjacent to each other. We’ll remove the second item from the list.
Finally, it’s tempting to recurse on the remainder of the list (lnk.rest
), but remember that there could still be more duplicates of the first item in the rest of the list! So we have to recurse onlnk
instead. Remember that we have removed an item from the list, so the list is one element smaller than before.
Normally, recursing on the same list wouldn’t be a valid subproblem. - Otherwise, there is no duplicate of the first item. We can safely recurse on the remainder of the list.
Iterative solution1
2
3
4
5while lst is not Link.empty or lst.rest is not Link.empty:
if lst.first == lst.rest.first:
lst.rest = lst.rest.rest
else:
lst = lst.restMidterm Review
3.1 Write a function that takes a list and returns a new list that keeps only the evenindexed elements of lst and multiplies them by their corresponding index.
1 | def even_weighted(lst): |
3.2 The quicksort sorting algorithm is an efficient and commonly used algorithm to order the elements of a list. We choose one element of the list to be the pivot element and partition the remaining elements into two lists: one of elements less than the pivot and one of elements greater than the pivot. We recursively sort the two lists, which gives us a sorted list of all the elements less than the pivot and all the elements greater than the pivot, which we can then combine with the pivot for
a completely sorted list.
First, implement the quicksort_list
function. Choose the first element of the list as the pivot. You may assume that all elements are distinct.
Note: in computer science, “sorting” refers to placing elements in order from least to greatest, not putting things in categories
1 | def quicksort_list(lst): |
3.3 Write a function that takes in a list and returns the maximum product that can be formed using nonconsecutive elements of the list. The input list will contain only numbers greater than or equal to 1.
1 | def max_product(lst): |
At each step, we choose if we want to include the current number in our product
or not:
- If we include the current number, we cannot use the adjacent number.
- If we don’t use the current number, we try the adjacent number (and obviously ignore the current number).
The recursive calls represent these two alternate realities. Finally, we pick the one that gives us the largest product.
3.4 Complete redundant_map
, which takes a tree t
and a function f
, and applies f
to each node ($2^d$) times, where d is the depth of the node. The root has a depth of 0. It should mutate the existing tree rather than creating a new tree.
1 | def redundant_map(t, f): |
Every time we recurse, we transform our map function into one that is one level
deeper in terms of calls to input function f. To see why this will achieve the result we want, let’s look at what happens to some input function f.
• The first call to redundant_map will call f once.
• This means on the second call to redundant_map, we pass in a function g that
causes the original f to be called two times.
• On the third call to redundant_map, we pass in a function h that causes g to
be called two times. Remember that g calls original f twice, so h will end up
calling original f four times.
Therefore, each level will have double the calls to f as the previous level, which
matches the requirements.