CS106 Summer 2026 Michael Eckmann Lab 08 July 15, 2026 ================== Purpose: To get familiar with the following topics understanding nested loops reading recursive functions writing recursive functions =========== PROBLEM 1 =========== Treat each one separately and do the following. Read the code by eye and type up a prediction of what will print. Run the code and see if the output agrees with your prediction. If it does not, try to figure out why and explain why it works the way it does. for i in range(4): for j in range(3): print(f'{i} {j}') for i in range(4): for j in range(i,5): print(f'{i} {j}') for i in range(4): for j in range(0,5-i): print(f'{i} {j}') mylist = [4,3,7,2] for i in range(len(mylist)): for j in range(0,len(mylist)-i): print(f'{i} {j}') =========== PROBLEM 2 =========== Put the code below in a program and run it and figure out what prints and why. If you need help understanding what the output is, I can help read through the program with you. def selection_sort(nums_list): for phase in range(1, len(nums_list)): idx_of_max = 0 print('pass #', phase) for i in range(1, len(nums_list) - phase + 1): print('comparing',nums_list[idx_of_max],' < ',nums_list[i]) if nums_list[idx_of_max] < nums_list[i]: print('idx of max changed to',i) idx_of_max = i temp = nums_list[idx_of_max] nums_list[idx_of_max] = nums_list[len(nums_list) - phase] nums_list[len(nums_list) - phase] = temp print('list is now:',nums_list) # main program: mylist = [12,50,-8,11] print(mylist) selection_sort(mylist) =========== PROBLEM 3 =========== #Given the following function, try to predict the result of the calls provided. #Then describe in general what mystery computes. def mystery(x,y): if x == y: return x else: return x + mystery(x+1, y) print(mystery(3,8)) print(mystery(1,9)) =========== PROBLEM 4 =========== #Given the following function, try to predict what will print when it is called. #Then describe in general what mystery2 computes. def mystery2(nums_list, x): if len(nums_list) == 0: print('returning 0') return 0 elif x == nums_list[0]: nums_list.remove(nums_list[0]) print('adding 1', len(nums_list)) return 1 + mystery2(nums_list, x) else: nums_list.remove(nums_list[0]) return mystery2(nums_list, x) mylis = [42,3,8,7,3,45,12,22,3,7] number = mystery2(mylis, 3) print(number) =========== PROBLEM 5 =========== Write a recursive function for power. First parameter represents the base and second represents the exponent. Note: can't use ** or math.pow in your code. a. first make it handle any exponents 0 or higher e.g. power(2,10) # should return 1024 b. change it to handle negative exponents power(2,-5) # should return 0.03125 (which is 1 / (2**5) ) =========== PROBLEM 6 =========== Write a program named multiply.py that includes 2 functions and some function calls. The two functions named mult_iterative and mult_recursive should each take in an int as a parameter representing an upper limit. The two functions should each multiply up all the odd numbers from 1 up to and possibly including that upper limit value (which is to be passed in as a parameter) and return that product. e.g. if I call mult_iterative(10) or mult_recursive(9) they should return the value 1*3*5*7*9 = 945 Notice the same answer should be generated whether 9 or 10 is the parameter. The iterative function should use a loop to compute the result and the recursive function should use recursion. Place some calls to each of the two functions in the main program and have the results printed out in the main program. --------- submit work to theSpring