Questions
This review is not comprehensive of all content that will be on the final exam, but rather provides a few extra practice questions and some questions on concepts after quiz 3. Previous quizzes/practice questions are also valuable in reviewing for the final exam. Lecture videos are another valuable resource.
Solutions for each problem can be found at the bottom of this page. Keep in mind that there may be multiple solutions to each problem.
Function Writing
Write a function called
reverse_multiply. Given alist[int],reverse_multiplyshould return alist[int]with the values from the original list doubled and in reverse order.
Example:reverse_multiply([1, 2, 3])should return[6, 4, 2].Write a function called
free_biscuits. Given a dictionary withstrkeys (representing basketball games) andlist[int]values (representing points scored by players),free_biscuitsshould return a new dictionary of typedict[str, bool]that maps each game to a boolean value for free biscuits. (Trueif the points add up to 100+,Falseif otherwise)
Example:free_biscuits({ “UNCvsDuke”: [38, 20, 42] , “UNCvsState”: [9, 51, 16, 23] })should return{ “UNCvsDuke”: True, “UNCvsState”: False }.Write a function called
multiples. Given alist[int],multiplesshould return alist[bool]that tells whether eachintvalue is a multiple of the previous value. For the first number in the list, you should wrap around the list and compare thisintto the last number in the list.
Example:multiples([2, 3, 4, 8, 16, 2, 4, 2])should return[True, False, False, True, True, False, True, False].Write a function called
merge_lists. Given alist[str]and alist[int],merge_listsshould return adict[str, int]that maps each item in the first list to its corresponding item in the second (based on index). If the lists are not the same size, the function should return an empty dictionary.
Example:merge_lists([“blue”, “yellow”, “red”], [5, 2, 4])should return{"blue": 5, "yellow": 2, "red": 4}.
Class Writing
- Create a class called
HotCocoawith the following specifications:- Each
HotCocoaobject has aboolattribute calledhas_whip, astrattribute calledflavor, and twointattributes calledmarshmallow_countandsweetness. - The class should have a constructor that takes in and sets up each of its attribute’s values.
- Write a method called
mallow_adderthat takes in anintcalledmallows, increases themarshmallow_countby that amount, and increases thesweetnessby that amount times 2. - Write a method called
calorie_countthat returns afloat. If theflavorof theHotCocoais “vanilla” or “peppermint”, increase the calorie count by 30, otherwise increase it by 20. If theHotCocoahas whipped cream (has_whipisTrue), increase the calorie count by 100. Finally, increase the calorie count by half the number of marshmallows. The calorie count should be calculated and returned when this method is called.
- Each
- Create a class called
TimeSpentwith the following specifications:- Each
TimeSpentobject has astrattribute calledname, astrattribute calledpurpose, and anintattribute calledminutes. - The class should have a constructor that takes in and sets up each of its attribute’s values.
- Write a method called
add_timethat takes in anintand increases theminutesattribute by this amount. The method should returnNone. - Write a method called
resetthat resets the amount of time that is stored in theminutesattribute. The method should return the amount that was stored inminutes. - Write a method called
reportthat prints a line reporting information about the currentTimeSpentobject. Suppose aTimeSpentobject hasname=“Ariana”,purpose=“screen time”, andminutes=130. The report method should print:“Ariana has spent 2 hours and 10 minutes on screen time.”
- Each
Recursion
- What is it that makes a function/structure recursive?
- A recursive function must have a recursive case. (T/F)
- A recursive function may define multiple recursive cases. (T/F)
- What happens if a recursive function does not make progress toward a base case?
- Write a recursive factorial function. The factorial of a positive integer is the product of that integer with all of the positive integers less than it.
!nis used to denote the factorial of a positive integern. (4! = 4*3*2*1 = 24) Ex:factorial(4)should return24,factorial(5)should return120,factorial(1)should return1. - Diagram the following call to
factorialusing the solution provided below. - EXTRA PRACTICE: Diagram calls to functions from EX10: Linked List Utils. For obvious reasons, we cannot provide solutions to these, but having practice diagramming recursive structures as well as recursive functions such as
factorialis great practice for the final! The examples in lecture videos can be helpful if this is difficult, and as always office hours is a great resource if you struggle with this.
Solutions
Function Writing
Class Writing
Recursion
- A recursive function is a function that calls itself, and recursive structures are defined in terms of themselves.
- T
- T
- If a recursive function does not make progress toward a base case, it will result in infinite recursion and python will stop its execution with a RecursionError.







