CMU 15-110 Fall 2018: Principles of Computing
Homework 3 (Due Tuesday 18-Sep at 8pm)



Team Hw3


Solo Hw3

  1. digitCount and hasConsecutiveDigits videos [0 pts]
    Before you start the solo hw, you should watch these videos. They contain detailed explanations of how to solve the first two exercises on the hw!
    • digitCount(n)
    • hasConsecutiveDigits(n)


  2. digitCount(n) [10 pts]
    Note: Even though you just watched the video explaining how to solve this, you should not directly copy the solution from the video. Instead, write it from scratch here. Of course, you'll get an answer very similar, or even identical, to the one in the video. That's fine. But you need to be able to do this yourself, without consulting the video. If you need help, of course, go to OH. We are here to help you!

    With that in mind, write the function digitCount(n) that takes a possibly-negative int and returns the number of digits in it. So, digitCount(12323) returns 5, digitCount(0) returns 1, and digitCount(-111) returns 3. One way you could do this would be to return len(str(abs(n))), but you cannot do that, since you may not use strings here! This can be solved with logarithms, but seeing as this is "loops week", you should instead simply repeatedly remove the ones digit until you cannot.


  3. hasConsecutiveDigits(n) [10 pts]
    Write the function hasConsecutiveDigits(n) that takes a possibly-negative int value n and returns True if that number contains two consecutive digits that are the same, and False otherwise.


  4. containsDigit(n, d) [40 pts]
    Write the function containsDigit(n, d) that takes two integers, n and d, where d is between 0 and 9 inclusive (so d is a single digit), and returns True if n contains the digit d and False otherwise. For example, containsDigit(12323, 3) returns True, and containsDigit(12323, 4) returns False. Note that you should not count leading 0's as in the number, so containsDigit(12, 0) returns False, but containsDigit(120, 0) returns True.


  5. hasIncreasingDigits(n) [40 pts]
    Hint: be sure to first watch the video for hasConsecutiveDigits, and to write that function before starting this one!

    Write the function hasIncreasingDigits(n) that takes an integer n and returns True if the digits in n increase from left to right.

    For example, consider the number 1247. From the left, we start with 1, and we note that 2>1 (good), 4>2 (good), and 7>4 (good), so each digit is larger than the one before it. So hasIncreasingDigits(1247) returns True.

    As another example, consider 1243. This proceeds as before: start with 1, note that 2>1 (good), 4>2 (good), but then we see that 3<4 (bad). So it is not true that each digit is larger than the one before it. So hasIncreasingDigits(1243) returns False.

    Here is a test function for you:
    def testHasIncreasingDigits(): print('Testing hasIncreasingDigits()... ', end='') assert(hasIncreasingDigits(0) == True) assert(hasIncreasingDigits(9) == True) assert(hasIncreasingDigits(10) == False) assert(hasIncreasingDigits(11) == False) assert(hasIncreasingDigits(12) == True) assert(hasIncreasingDigits(1225) == False) assert(hasIncreasingDigits(1235) == True) assert(hasIncreasingDigits(24579) == True) assert(hasIncreasingDigits(245079) == False) assert(hasIncreasingDigits(-24579) == True) # sign doesn't matter print('Passed.')

  6. Bonus/Optional: longestDigitRun(n) [2.5 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).