Questions
This practice is intended to help you become more comfortable with code writing style questions, though the final will have other types of questions as well.
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_multiply
should 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 withstr
keys (representing basketball games) andlist[int]
values (representing points scored by players),free_biscuits
should return a new dictionary of typedict[str, bool]
that maps each game to a boolean value for free biscuits. (True
if the points add up to 100+,False
if 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]
,multiples
should return alist[bool]
that tells whether eachint
value is a multiple of the previous value. For the first number in the list, you should wrap around the list and compare thisint
to 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_lists
should 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
HotCocoa
with the following specifications:- Each
HotCocoa
object has abool
attribute calledhas_whip
, astr
attribute calledflavor
, and twoint
attributes calledmarshmallow_count
andsweetness
. - The class should have a constructor that takes in and sets up each of its attribute’s values.
- Write a method called
mallow_adder
that takes in anint
calledmallows
, increases themarshmallow_count
by that amount, and increases thesweetness
by that amount times 2. - Write a method called
calorie_count
that returns afloat
. If theflavor
of theHotCocoa
is “vanilla” or “peppermint”, increase the calorie count by 30, otherwise increase it by 20. If theHotCocoa
has whipped cream (has_whip
isTrue
), 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
TimeSpent
with the following specifications:- Each
TimeSpent
object has astr
attribute calledname
, astr
attribute calledpurpose
, and anint
attribute calledminutes
. - The class should have a constructor that takes in and sets up each of its attribute’s values.
- Write a method called
add_time
that takes in anint
and increases theminutes
attribute by this amount. The method should returnNone
. - Write a method called
reset
that resets the amount of time that is stored in theminutes
attribute. The method should return the amount that was stored inminutes
. - Write a method called
report
that prints a line reporting information about the currentTimeSpent
object. Suppose aTimeSpent
object hasname
=“Ariana”
,purpose
=“screen time”
, andminutes
=130
. The report method should print:“Ariana has spent 2 hours and 10 minutes on screen time.”
- Each