CMU 15-110 Spring 2019: Principles of Computing
Homework 3 (Due Thursday 21-Feb at 8pm)


This hw is entirely solo. See the syllabus for details.
To start, download these two files:
  1. hw3.py
  2. hw3-words.txt
Be sure to place them in the same folder. If pyzo still cannot find hw3-words.txt, then do this: Go to Shell/Edit Shell Configurations, find the startDir field, and enter a single period there (.), and press the Done button. Then restart pyzo. If that still does not work, go to OH and the TA's will happily help you (as always!).
  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.

Important Note: the next 3 functions are all related to a Guess-the-Word Game that you will write later on as part of this hw. If it helps, you can watch the video (see below) now, but you should not write the game until you have first completed these functions.
  1. isWordGuessed(word, guesses) [10 pts]
    Write the function isWordGuessed(word, guesses) that takes two strings, a word and the letters guessed so far, and returns True if the guesses include all the letters in the word, and False otherwise. For example, isWordGuessed('egg', 'gq') returns False because 'e' is in 'egg' but 'e' has not yet been guessed. Similarly, isWordGuessed('egg', 'gqe') returns True because all the letters in 'egg' are included in the guesses. Note: you may assume there are no uppercase letters in word or guesses.

  2. makeDisplayWord(word, guesses) [10 pts]
    Write the function makeDisplayWord(word, guesses) that takes two strings, a word and guesses, just as in isWordGuessed(word, guesses) above, only this function returns a new string, the so-called display word. This is the same as the word, only those letters in the word that are not yet guessed are replaced with dashes. So, for example, makeDisplayWord('egg', 'gq') returns '-gg', where the 'e' was replaced with a '-' because it was not yet guessed. Similarly, makeDisplayWord('egg', 'gqe') returns 'egg' because all the letters were guessed. You may again assume no uppercase letters are in word or guesses.

  3. getErrorMessage(guess, word, guesses) [10 pts]
    Write the function getErrorMessage(guess, word, guesses) that takes three strings -- the current guess (a single letter) and the word and guesses as in the previous two functions -- and returns a string containing the error message, if any, that the game should display in response to this new guess. If the guess is not already in the guesses, and it is in the word, that means the user made a good guess, so just return the empty string (''). Otherwise, if the guess is a duplicate (so it is already in the guesses), then return 'You already guessed [guess]' (where [guess] is replaced by their guess). If it's not a duplicate, but it is not a letter (which you can test using isalpha()), then return '[guess] is not a letter!' (again where [guess] is replaced by their guess). Otherwise, just return '[guess] is not in the word!' (again where [guess] is replaced by their guess). So, for example:
       getErrorMesssage('e', 'egg', 'gq') returns ''
       getErrorMesssage('g', 'egg', 'gq') returns 'You already guessed g'
       getErrorMesssage('@', 'egg', 'gq') returns '@ is not a letter!'
       getErrorMesssage('z', 'egg', 'gq') returns 'z is not in the word!'
    

Note: the next part (the game) is not autograded.
  1. Guess-the-word Game [50 pts]
    First, be sure to have successfully completed all the exercises above (because you will need those functions here!). Then, be sure to carefully watch this helpful that explains the Guess-the-word Game you will write here.

    Note: when we grade this game, we will not test any special keys (such as 'Up' or 'Tab', etc). We will only test using letters, digits, and punctuation. However, if you wish to make these work properly, you might just test if (len(event.key) == 1).

    To get started, after you watch the video, be sure to write your code for this game in hw3.py, in the place we already provided for you in the file below the #ignore_rest line so the autograder does not get confused by it (since this part is not autograded). Have fun!

    Hint: we included init(data) in the starter file -- you are welcome to edit that if you wish, though there is no need to do so. The only two functions you have to write here are keyPressed(event, data) and drawAll(canvas, data).

Note: the next function is bonus/optional.
  1. 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).

  2. Bonus/Optional: guess-the-word graphics [up to 2 pts]
    The guess-the-word game from above often comes with some sort of graphics, where a picture is drawn in pieces. Each time you miss a letter, another part of the picture is drawn. When the entire picture is drawn, you lose. Similarly, you could start with a complete picture and remove parts, and lose when the picture is entirely gone. Or something like that, only different. You get the idea. Mainly for fun, but also for up to 2 pts of bonus, you can add some guess-the-word graphics of this kind to your hw3 submission. Be really clever and have fun with it!