CMU 15-112 Fall 2015 Homework 2 Practice
(Due never)



  1. mostFrequentDigit(n)
  2. isRotation(x, y)
  3. hasConsecutiveDigits(n)
  4. longestDigitRun(n)
  5. nthPalindromicPrime(n)
  6. nthLeftTruncatablePrime(n)
  7. nthPowerfulNumber(n)
  8. carrylessAdd(x, y)
  9. countingPrimes
  10. findZerosWithBisection
  11. And More!

  1. mostFrequentDigit(n)
    Write the function mostFrequentDigit(n), that takes a non-negative integer n and returns the digit from 0 to 9 that occurs most frequently in it, with ties going to the smaller digit.

  2. isRotation(x, y)
    Write the function isRotation(x, y) that takes two non-negative integers x and y and returns True if x is a rotation of the digits of y and False otherwise. For example, 3412 is a rotation of 1234, and 321 (with an implicit leading 0) is a rotation of 3210. Any number is a rotation of itself.

  3. hasConsecutiveDigits(n)
    Write the function hasConsecutiveDigits(n) that takes a possibly-negative integer n and returns True if n has two consecutive equal digits, and False otherwise.

  4. longestDigitRun(n)
    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).

  5. nthPalindromicPrime(n)
    Write the function nthPalindromicPrime(n). See here for details. So nthPalindromicPrime(0) returns 2, and nthPalindromicPrime(10) returns 313.

  6. nthLeftTruncatablePrime(n)
    Write the function nthLeftTruncatablePrime(n). See here for details. So nthLeftTruncatablePrime(0) returns 2, and nthLeftTruncatablePrime(10) returns 53.

  7. nthPowerfulNumber(n)
    Write the function nthPowerfulNumber(n). See here for details. So nthPowerfulNumber(0) returns 1, and nthPowerfulNumber(10) returns 64.

  8. carrylessAdd(x, y)
    First, read the first page (page 44) from here about Carryless Arithmetic. Fun! Then, 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.

  9. countingPrimes
    Do the "Counting Primes" problem here.

  10. findZerosWithBisection
    Do the "Find Zeros With Bisection" problem here.

  11. And More!
    You can find even more loops-and-conditional problems here.