Homework 2
Due Tuesday 24-Jan, at 10:00pm
To start
- Create a folder named ‘hw2’
- Download hw2.py to that folder
- Edit hw2.py and modify the functions as required
- When you have completed and fully tested hw2, submit hw2.py to Gradescope.
For this hw, you may submit up to 15 times, but only your last submission counts.
Some important notes
- This homework is solo. You may not collaborate or discuss it with anyone outside of the course,
and your options for discussing with other students currently taking the course are limited.
See the academic honesty policy for more details.
- After you submit to Gradescope, make sure you check your score. If you aren’t sure
how to do this, then ask a CA or Professor.
- There is no partial credit on Gradescope testcases. Your Gradescope score is your Gradescope
score.
- Read the last bullet point again. Seriously, we won’t go back later and increase
your Gradescope score for any reason. Even if you worked really hard and it was only a
minor error…
- Do not hardcode the test cases in your solutions.
- The starter
hw2.py
file includes test functions to help you test on your own before
you submit to Gradescope. When you run your file, problems will be tested in order. If
you wish to temporarily bypass specific tests (say, because you have not yet completed
some functions), you can comment out individual test function calls at the bottom
of your file in main()
. However, be sure to uncomment and test everything together
before you submit! Ask a CA if you need help with this.
- Remember the course’s academic integrity policy. Solving the homework yourself is
your best preparation for exams and quizzes; cheating or short-cutting your learning
process in order to improve your homework score will actually hurt your course grade
long-term.
- Do not use strings, lists, list indexing, or recursion this week. In particular, you are not allowed to convert numbers to strings. The
autograder (or a manual CA review later) will reject your submission entirely if you do.
Problems
-
numberLength [5 pts]
Write the function numberLength(x)
that takes a non-negative integer x
and returns the number of digits in x
. For example, numberLength(12)
would return 2.
Note: This function may be useful in other problems in this assignment...
-
containsOddDigits [5 pts]
Write the function containsOddDigits(x)
that takes an integer, x
, and returns True
if it contains any odd digits, and False
otherwise.
Hint: In this and in future problems, you'll need to get individual digits from numbers. You may want to reuse your solution to getKthDigit
and numberLength
to do this.
-
printNumberTriangle [10 pts]
Time for something a little different! Instead of returning a value, in this function you'll be printing out results. Write the function printNumberTriangle(n)
that takes one integer, n
, and prints out a number triangle based on n
. For example, given the number 4, this function would print out
1
21
321
4321
Note: the autograder for this problem is very finicky about whitespace; it expects no spaces, and only one newline after each number line. Make sure your output matches what it expects!
-
longestDigitRun [15 pts]
Write the function longestDigitRun(n)
that takes a possibly-negative int value n
and returns the digit that has the longest consecutive run, or the smallest such digit if there is a tie. So, longestDigitRun(117773732)
returns 7 (because there is a run of 3 consecutive 7's), as does longestDigitRun(-677886)
.
-
isPalindromicNumber(n) [15 pts]
Write the function isPalindromicNumber(n)
that takes a non-negative
integer n
and returns True
if that number is palindromic and
False
otherwise. A palindromic number is the same forwards
as backwards. For example, these numbers are palindromic: 0, 1,
99, 12321, 123321, and these numbers are not: 1211, 112, 10010.
-
carrylessAdd(x, y) [20 pts]
First, you may wish to read the first page (page 44) from
here about Carryless Arithmetic.
Or, just understand that carryless addition is what it sounds like --
regular addition, only with the carry from each column ignored.
So, for example, if we carryless-ly add 8+7, we get 5 (ignore the carry).
And if we add 18+27, we get 35 (still ignore the carry).
With this in mind, write the function carrylessAdd(x, y)
that takes two non-negative integers x
and y
and returns their carryless sum. As the paper demonstrates, carrylessAdd(785, 376)
returns 51.
-
nthCircularPrime [30 pts]
A circular prime is a number with the property that any rotation of that number's digits is prime. In this case, rotation refers to cycling the digits of a number; for example, the rotations of 1234 are 1234, 2341, 3412, and 4123. You can read more about this on the Wikipedia page. Single-digit primes are all circular, of course. To find the nth circular prime, you'll need to write isPrime
and three other functions:
- rotateNumber [10 pts]
This function takes a number, x
, and rotates that number's digits by one place. This would turn the number 1234 to 4123.
- isCircularPrime [10 pts]
This function takes a number, x
, and determines whether that number is a circular prime. To do this, you'll need to check whether every rotation of the number is prime.
- nthCircularPrime [10 pts]
This function takes a number, n
, and returns the nth circular prime.