.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 10am CST on Tuesday, October 19, 2021.
This homework focuses dispatch tables, operators on iterables (e.g. any(), all()), and modules.
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:
modules/
subdirectory; the project 1 solution may also be helpful)any()
and all()
are often used with list comprehensions, you may want to consult: Lecture 8 - list methods and list comprehensionsThis homework assignment has 2 problems, numbered 2 and 3. Problem 2 is a bit longer than usual, so it gets 8 points this time. Thus the grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
8 | Problem 2 |
4 | Problem 3 |
14 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
Write a module digitprop
(contained in a file digitprop.py
) that contains two functions. The function definitions and docstrings you should use are shown below. Write the functions to do what the docstrings describe. Include a module docstring as well.
Restricted methods note: For full credit you must use any()
or all()
in an appropriate way in each function. You may need to use enumerate()
, too.
The module shouldn't do anything other than define functions when it is imported.
Checklist for grading:
digitprop.py
?all()
or any()
in an essential way?Advice: The function bodies, excluding the docstrings, can be just one or two lines each.
# Function definitions and docstrings to copy into your own `digitprop.py`
# Then, add function bodies!
def has_small_digits(n,maxdigit):
"""
Determines whether or not the digits of `n` are all between 0 and `maxdigit` (inclusive).
Returns `True` or `False` accordingly.
e.g.
has_small_digits(1021,1) returns False - third digit is larger than 1
has_small_digits(1021,2) returns True - all digits between 0 and 2
has_small_digits(1021,5) returns True - all digits between 0 and 5
has_small_digits(351622,5) returns False - fourth digits if larger than 5
has_small_digits(351622,6) returns True - all digits between 0 and 6
"""
def is_antipalindrome(n):
"""
Takes a positive integer `n`.
Returns `True` if reversing the order of the digits in `n` gives the same
result as replacing each digit d of `n` with 9-d ("flipping" the digits).
Otherwise, returns `False`.
(A number `n` for which this function returns `True` might be called an *antipalindrome*.)
e.g.
is_antipalindrome(5128) returns False, because reversing order gives 8215 while flipping digits gives 4871.
is_antipalindrome(4815) returns True, because reversing order or flipping digits each gives 5184.
"""
For the purposes of this problem, let's say that an integer is special if it is either an antipalindrome (in the sense of problem 2) or if it only uses the digits 0, 1, 2.
For example, 1012, 212000, and 4815 are all special, while 3012 and 5128 are not special.
Write a program hwk9prob3.py
that imports the digitprop
module you wrote in problem 2 and uses it to answer the following:
Question.
Is
d**k
ever special when
d
is a non-special number between 100 and 350 andk
is between 2 and 350 ?
In this problem, "between" has the inclusive meaning, so e.g. k
is allowed to be 2
or 350
.
If the program finds any of these, it should print d
, k
, and d**k
. After running the program, paste the results you found as comments at the bottom of hwk9prob3.py
.
Make sure the program has a file-level docstring.
Checklist for grading grading:
digitprop
rather than replicating any of its features?d
,k
,d**k
) included in comments at the bottom of the program?Note: A correct program will take a some time to run, since it will need to examine about 81,000 integer powers.