.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 15 February 2022.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
This homework assignment has two problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.
Ask your instructor or TA a question by email, in office hours, or on discord.
In Gradescope, the score assigned to your homework submission by the autograder (checking for syntax and docstrings) will be recorded as "Problem 1". Therefore, the numbering of the actual problems begins with 2.
In Python, you can check the current day and time by importing the datetime
module and then calling datetime.datetime.now()
. The return value is an object with attributes and methods to tell you things like the year, month, day, hour, minute, second, and day of week. For example, this script will print the current hour and weekday:
import datetime
dt = datetime.datetime.now()
print("Right now, the hour (in 24-hour time) is: ",dt.hour)
day_number = dt.weekday() # 0 for Monday, 1 for Tuesday, etc.
day_names = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
print("And the day of week is {}, which is day number {} in Python's numbering system.".format(
day_names[day_number],
day_number
))
Write a context manager WorkingHours()
that makes sure a block of code starts running between 9am and 5pm on a Monday, Tuesday, Wednesday, Thursday, or Friday. For the purposes of this question, we'll call those "working hours". So the desired behavior of this context manager is:
__enter__
is called during working hours, it should return immediately__enter__
is called at any other time, it should pause for 60 seconds, check again, etc., not returning until it is during working hours.This context manager doesn't need to do any cleanup at the end of the with
block, but it still needs to have a method called __exit__
in order for Python to recognize it as a context manager.
Put your context manager class in a file called hwk5prob2.py
and upload it to gradescope.
Consider the three functions below. They call one another in a rather complicated way!
def alpha(x):
if x<0:
return 0 # a
elif x%7 < 3:
return beta(x-1) # b
else:
return gamma(x-1) + beta(x//3) # c
def beta(x):
if x%9 < 5:
return alpha(x-2) # d
else:
return gamma(x-1) + alpha(2*x-4) # e
def gamma(x):
if x%275 > 10:
return x**abs(x) % 13 # f
else:
return alpha((9*x)//10) # g
Use any debugging techniques you like (pdb
, print
, etc.) to answer the following questions. Type your answers in a file called hwk5prob3.txt
that you create using Visual Studio Code, and then upload that file to gradescope. Note that you're not uploading any Python code for this question, you're just typing answers into a file and uploading that file.
Calling alpha(24)
returns 14, as you can check. In doing so, how many many times is each function alpha
, beta
, and gamma
called? (Give a separate total for each one.)
In computing alpha(24)
, what is the largest value of x
that is passed to any of the functions?
Some of the lines of code contain comments labeling them with the letters a-g. In computing alpha(24)
, does each labeled line of code run at least once? If not, which of them never run?
Are there any other values of x
between 0 and 100 so that alpha(x)
returns 14? (Some values of x
will cause a RecursionError
exception to be raised, so you'll need to account for that in automating a search.)