How many steps are required for each of the following?
#############################################
for i in range(N):
for j in range(N):
print(i, j)
#############################################
while N > 1:
print(N)
N = N/2
#############################################
for i in range(N):
for j in range(10):
print(i, j)
#############################################
Consider the following function, oneLoop(L)
, which is similar to the inner loop from bubble sort:
Write an expression for the asymptotic runtime of oneLoop
, in terms of the length n of the list L
.
Show how the following list would be changed after selection sort does its first 3 swaps:
Show how the following list would be changed after bubble sort does one iteration of the outer loop.
Which algorithm requires time directly proportional to the size of the input list?
Write a recursive function longestInList(L)
that returns the longest string in L
, a list of strings.
Then, draw the stack as it would look at its largest (i.e, the deepest part of the recursion when it hits a base case), when the function is called on the list ['awesome', 'recursive', 'code']
. pdf of solution
Write a recursive function countdown(n)
which simulates a countdown to a rocket launch. If you call blastoff(4)
you should see the following output:
This function is only called for its side-effects (printing), and not its return value.
Write a recursive function recBinary(x,L)
which implements a recursive version of binary search for item x
in list L
. It should return True
if x
is in L
, and False
otherwise.
For additional recursive function problems, try problems on this page.