CMU 15-112 Fall 2017: Fundamentals of Programming and Computer Science
Homework 2 (Due Saturday 9-Sep, at 8pm)
- This hw is SOLO. See the syllabus for details.
- To start:
- Create a folder named 'week2'
- Download both
hw2.py
and
cs112_f17_week2_linter.py
to that folder
- Edit hw2.py using Pyzo
- When you are ready, submit hw2.py to Autolab. For this hw, you may submit up to 10 times,
but only your last submission counts.
- Do not use strings, lists, or recursion this week. Note that the linter will not check for strings (since they ARE allowed in the bonus problem), but you still may not use them outside of the bonus!
- Do not hardcode the test cases in your solutions.
- 1-Hour TA-Led Practice Session [20 pts]
For this exercise, you need to attend a 1-hour week2 practice session with one of your TA's from your assigned recitation. Your TA's will contact you via email with numerous different times when they will offer these 1-hour sessions. You need to attend one of these by 8pm Saturday night. The sooner, the better, as this will be a great help for you prior to starting hw2. Note: you only get credit for attending if you are on time and stay the whole time. You have nothing to submit for this -- the TA's will take attendance and will enter this directly into Autolab, so you then receive the points for attending. Never mind the points, though: this is an invaluable time to practice with an expert, and to let you see just how wonderfully helpful it is to work with our TA's!
- 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. If you want, you can reuse your solutions to getKthDigit from lab1 and numberLength to do this.
- countMultiplesOfSeven [20 pts]
Write the function countMultiplesOfSeven(a, b) that takes two integers, a and b, and returns the number of multiples of seven that occur between a and b (including a and b). For example, countMultiplesOfSeven(4, 16) would return 2, since two multiples of 7 (7 and 14) occur within that range.
- printNumberTriangle [20 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 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!
- nthHappyPrime [30 pts]
Write the function nthHappyPrime(n) that, of course, finds the nth happy prime. Prime we know already, but what is a happy number? To find out, read the first paragraph on the Wikipedia page. For our purposes, we can simplify the process of finding a happy number by saying that a cycle which reaches 1 indicates a happy number, while a cycle which reaches 4 indicates a number that is unhappy. To solve this problem, you'll want to use isPrime and write three other functions:
- sumOfSquaresOfDigits [10 pts]
Write the function sumOfSquaresOfDigits(n) which takes a non-negative integer and returns the sum of the squares of its digits. For example, 123 would become 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.
- isHappyNumber [15 pts]
Write the function isHappyNumber(n) which takes a possibly-negative integer and returns True if it is happy and False otherwise. Note that all numbers less than 1 are not happy.
- nthHappyPrime [5 pts]
A happy prime is a number that is both happy and prime. Write the function nthHappyPrime(n) which takes a non-negative integer and returns the nth happy prime number (where the 0th happy prime number is 7).
- Bonus/Optional: play112 (The 112 Game) [3 pts]
Read the writeup for "The 112 Game"
here (skip to Part B). Be sure to follow the spec exactly, so the autograder can properly grade your work! As with all bonus, we recommend that you only do this for the joy of learning (which is great), and not for the points (which are modest). Also, you may use strings in this problem, but ONLY this problem.
To accomodate this, you should download and use the following linter: cs112_f17_hw2_linter.py.