Activities: Week 5

Problem Set

This problem encountered technical difficulties in our textbook, so codelens shows the correct result of invoking this function, but running the function in the textbook does not always. As a result, EVERYONE will get some free points for this problem. You do not need to worry about solving it, but it is worth spending a little bit of time checking it out and thinking about it!

Below is a function definition. DO NOT change it!

We have also provided some invocations of that function. Run those and see what they do.

Below the comment provided in the code window, write a few calls to this function yourself, with whatever appropriate input you want.

Finally, write a few sentences in comments in the code window that explain what’s happening in this function called list_end_with_string. You should explain what happens if a list like l gets input into this function AND what happens if a list like b gets input into it.

Don’t forget to run it and save!

We’ve given you another data file in this problem. It’s called timely_file.txt. Write code to figure out which is the most common word in the file. Save the string that is most common word in the file in the variable abc. (Hint: you had a problem quite similar to this one in PS3!) You may write a function to help do this, AND invoke the function, if you do so – but you do not have to.
Define a function is_prefix that takes two strings as inputs and returns the boolean value True if the first string is a prefix of the second string, but returns the boolean value False otherwise. You can assume the first string will always be shorter than, or the same length as, the second string.

In the next few questions, you’ll build components and then a complete program that lets people play Hangman.

Below is an image from the middle of a game…

Assignments/Figures/HangmanSample.JPG

Your first task is just to understand the logic of the program, by matching up elements of the flow chart above with elements of the code below. In later problems, you’ll fill in a few details that aren’t fully implemented here.

You may find it helpful to run this program in order to understand it. It will tell you feedback about your last guess, but won’t tell you where the correct letters were or how much health you have in the game, and it won’t stop if you guess all the letters, so you can’t really play with this version of the code here. (It can also go on for a very long time, until you exceed the time limit in the code window, unless you cancel it yourself.) Allowing the game to do those things (manage health, show you the word you’ve guessed so far) comes from code you will write in later problems!

The next few questions are based on this code/set of ideas. This is the base code for a Hangman game, without some of the important useful functionality, as you’ll be building it! (If you have never played Hangman, you can go to https://en.wikipedia.org/wiki/Hangman_(game) for an explanation of what it is.) There is (or will be) a flow chart image of the process of this code also available, which may be helpful to refer to as you try to understand the code.

See the flow chart below for a better understanding of what’s happening in the code for the Hangman game overall. Your first task is just to understand the logic of the program, by matching up elements of the flow chart above with single numeric lines of the code below (which line of code corresponds to the box?). Answer in comments, below. Each answer should be no more than 2 numbers that represent lines of code. Each question can be answered with 1 or 2 line numbers!

In later problems, you’ll fill in a few details that aren’t fully implemented in the code above.

Assignments/Figures/HangmanFlowchart.jpg

The next task you have is to create a correct version of the blanked function. It should take 2 inputs: a word, and a string of the letters that have been guessed already.

It should return a string with the same number of characters as the word, but with the UNrevealed characters replaced by an underscore (a _).

HINT: Iterate through the letters in the word, accumulating characters as you go. If you try to iterate through the guesses, it’s harder.

Now you have to create a good version of the health_prompt function: Define a function called health_prompt. The first parameter should be the current health the player has (an integer), and the second parameter should be the maximum health a player can have (an integer). The function should return a string with + signs for the current health, and - signs for the health that has been lost so far.

In class, you’ll see these things all put together. Soon, you’ll put these together yourself and run your completed hangman program on your own computer, instead of in the textbook.

To run the whole program, with the functions you built in questions above, paste the health_prompt function and the blanked function at the very top of the code box provided below:

This is the base code for a Hangman game, without some of the important useful functionality – but now, you can add it in! (If you have never played Hangman, you can go to https://en.wikipedia.org/wiki/Hangman_(game) for an explanation of what it is.) You should paste or retype your functions in where indicated at the top, and try running this, for points on this problem.
Next Section - Activities: Week 6 (through fall break)