CS106 Summer 2026 Michael Eckmann Lab 06 July 13, 2026 ================== Purpose: To get familiar with the following topics reading code that contains calls to programmer-defined functions writing more programmer-defined functions understanding how to call two functions and what is required to make them work correctly =========== PROBLEM 1 =========== Take a look at the following program and address the lettered questions in each of the comments. def compute_factorial(num): temp_factorial = 1 # line 1 while num > 0: # line 2 temp_factorial *= num # line 3 num -= 1 # line 4 # what is the value of num here? return temp_factorial # line 5 # start of main program number = 3 # line 6 fact_of_that_number = compute_factorial(number) # line 7 print('The factorial of', number, 'is', fact_of_that_number) # line 8 number = 2 # line 9 fact_of_that_number = compute_factorial(number) # line 10 print('The factorial of', number, 'is', fact_of_that_number) # line 11 1.a. The program has comments to the right of lines of code that state the line number. Tell me what lines get executed in what order for this program, by listing the line numbers from the first to the last in order of execution. NOTE WELL: Some lines will be repeated and those that do, should appear in your list multiple times. HINT: line 6 gets executed first 1.b. If I changed the code to initialize temp_factorial to 0, what effect would that have on the program. Please explain in your own words in English. =========== PROBLEM 2 =========== # this function expects an int to be passed in # and returns True if that int is a number between 1 and 12 inclusive # and returns False otherwise def valid_month(the_month): result = False if the_month >= 1 and the_month <= 12: result = True return result I wrote valid_month for you above. Write four additional functions for this problem named: valid_day, valid_year, valid_date, number_of_days. All code for these functions ignores leap years. I am requiring you to use the defs for these function that I started for you below. def number_of_days(the_month): # your code here to return 28, 30 or 31 depending on the value of the_month # always assume February has 28 days # e.g. these calls: numdays = number_of_days(2) # numdays should be 28 because in a non-leap year, February has 28 days numdays = number_of_days(1) # numdays should be 31 (because January has 31 days) numdays = number_of_days(9) # numdays should be 30 (because September has 30 days) # return True if the_day is valid for the_month, # that is it is between 1 and the number of days in the_month def valid_day(the_day, the_month): # note: you should call number_of_days function inside this function # an integer >= 1583 should cause this function to return True # allow future years (i.e. >= 2026) for this one (different from the assignment) # only return False if the_year is < 1583 def valid_year(the_year): # return True if the_day, the_month and the_year makes a valid date def valid_date(the_day, the_month, the_year): # call valid_day, valid_month and valid_year within this function # if all 3 return True then return True # if any return False then return False =========== PROBLEM 3 =========== Put all the functions from problem 2 into one program named datevalidation.py Additionally write code to get the 3 ints from user input and call valid_date with those ints and print "valid date" or "invalid date". When writing the code for user input name your variables something other than the_day, the_month, and the_year so I know that you know they do not have the be the same names as in the parameters of the functions. =========== PROBLEM 4 =========== Create an application named searching.py that contains the functions binary_search and linear_search (code should be copy/pasted from below) Use the following line: list_of_nums = [ 240, 321, 333, 987, 90, 876, 12, 132, 564, 654, 241, 322, 334, 988, 91, 877, 13, 133, 565, 655, 242, 323, 335, 989, 92, 878, 14, 134, 566, 656 ] call binary_search and linear_search each twice to look for the number 777 and the number 134 Write if else statements and print statements that prints something like the following: The value __ was found at index __ OR The value __ was not found. Use the if/else print code after each call to one of the search functions. ( A total of 4 lines printed because you're searching for two different numbers in two different ways.) Did the functions calls do what you expected them to do? If not, why not? If so, why? Add code to fix the problem. Explain what you did to fix the problem. # linear search implementation # returns the index at which the key was found in the list # returns -1 if the key was not found def linear_search(nums_list, key): answer = -1 for i in range(len(nums_list)): if nums_list[i] == key: answer = i break return answer # binary search implementation # returns the index at which the key was found in the list # returns -1 if the key was not found def binary_search(nums_list, key): low_idx = 0 high_idx = len(nums_list) - 1 answer = -1 # -1 means we didn't find the key while low_idx <= high_idx: middle_idx = (low_idx + high_idx) // 2 if key == nums_list[middle_idx]: answer = middle_idx break elif key < nums_list[middle_idx]: high_idx = middle_idx - 1 else: low_idx = middle_idx + 1 return answer =========== PROBLEM 5 =========== Copy your datevalidation.py to datevalidationleap.py and add code to the datevalidationleap.py to handle leap years. Write a leap_year function with the following def def leap_year(the_year): # return True if the_year is a leap year, return False otherwise # see leap year rules in programming assignment 1 Add an additional parameter to valid_day function like so: def valid_day(the_day, the_month, the_year): and within the valid_day if the_month is 2 call the leap_year function and code to allow the_day to be as high as 29 if it is a leap year. ================ What to submit ================ Show me your answers and submit to the Spring