Homework 2

Due Tuesday 25-Jan, at 10:00pm


To start

  1. Create a folder named ‘hw2’
  2. Download hw2.py to that folder
  3. Edit hw2.py and modify the functions as required
  4. 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

  1. 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.
  2. 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.
  3. There is no partial credit on Gradescope testcases. Your Gradescope score is your Gradescope score.
  4. 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…
  5. Do not hardcode the test cases in your solutions.
  6. 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.
  7. 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.
  8. Do not use string indexing, lists, list indexing, or recursion this week. The autograder (or a manual CA review later) will reject your submission entirely if you do.

A Note About Testing

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.)


Problems


  1. 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...

  2. containsOddDigits [10 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 from homework 1 and numberLength to do this.

  3. hasConsecutiveDigits(n) [15 pts]
    Write the function 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.

  4. Helper Functions [15 pts]

    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.

      For example:
      • 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.

      For example:
      • removeLeftkDigits(1234,2) should return 34

      • removeLeftkDigits(1234,3) should return 4

      • removeLeftkDigits(1234,5) should return 0

      • removeLeftkDigits(0,1) should return 0


  5. longestCommonDigitStart [25 pts]
    Write the function 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.
    Here are a few more examples:
    longestCommonDigitStart(15112, 15112) == 15112 longestCommonDigitStart(763426548, 7346548) == 6548 longestCommonDigitStart(973492739487234, 1) == None longestCommonDigitStart(10, 20) == 0

  6. Kaprekar Numbers [30 pts]
    A Kaprekar number is a non-negative integer, the representation of whose square can be split into two possibly-different-length parts (where the right part is not zero) that add up to the original number again. For instance, 45 is a Kaprekar number, because 45**2 = 2025 and 20+25 = 45. You can read more about Kaprekar numbers here. The first several Kaprekar numbers are: 1, 9, 45, 55, 99, 297, 703, 999 , 2223, 2728 With this in mind,
    • 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.