Due Tuesday 25-Jan, at 10:00pm
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.You will notice that the skeleton file only includes testcases for some of the functions you are writing. You should write testcases for the others. (You can find some nice ways to test in the write-up below, but you will need to translate those to actual testcases.)
numberLength(x)
that takes a non-negative integer x and returns the number of digits in x. For example, numberLength(12) would return 2.containsOddDigits(x)
that takes an integer, x, and returns True
if it contains any odd digits, and False
otherwise.getKthDigit
from homework 1 and numberLength
to do this.
hasConsecutiveDigits(n)
that takes a
possibly-negative int n and returns True
if somewhere in n
some digit occurs consecutively (so the same digit occurs
twice in a row), and False
otherwise. For example,
these numbers have consecutive digits: 11, -543661, 1200,
-1200, and these numbers do not: 1, 123, 12321, -12321.
In this section, we ask you to write functions that can be useful in solving parts of later tasks. We hope that you will take advange of these functions, and other functions defined in this homework, for the solution to those tasks. For these problems, you CANNOT use string functions or convert integers to strings.
Write a function called getLeftkDigits(n, k)
that takes some number of digits from the left part of an integer and returns that value. This function should take two arguments, n and k, where n is the integer value and k is the number of digits to extract from the left. You can assume that n will always be a non-negative integer.
getLeftkDigits(1234,2) should return 12
getLeftkDigits(1234,3) should return 123
getLeftkDigits(1234,5) should return 1234
getLeftkDigits(0,1) should return 0
Write a function called removeLeftkDigits(n, k)
that removes some digits from the left part of an integer and returns the remaining digits as an integer. This function should take two arguments, n and k, where n is the integer value and k is the number of digits to remove from the left. You can assume that n will always be a non-negative integer.
removeLeftkDigits(1234,2) should return 34
removeLeftkDigits(1234,3) should return 4
removeLeftkDigits(1234,5) should return 0
removeLeftkDigits(0,1) should return 0
longestCommonDigitStart(x, y)
that takes two
non-negative integers, x and y, and returns the digits that match between the
two integers, starting from the ones digit. For examples, the pair (1234, 2134)
returns 34 because two digits match from right to left, 4 with 4 and 3 with 3.
The pair (2223, 23) also has two matches, and the solution is 23. If
there's no common digit start, the function should return None
. For
example, the pair (1234,4321) has no common start, and the result should
be None
.Write the function isKaprekarNumber(n)
that takes a non-negative integer n and returns True
if n is a Kaprekar number and False
otherwise.
Write the function nthKaprekarNumber(n)
that takes a non-negative int n and returns the nth Kaprekar number, where as usual we start counting at n==0 - so 0th Kaprekar number is 1, 1st Kaprekar number is 9 and so on.