Skip to content

Commit

Permalink
pip2 ps6 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
presnick committed Oct 5, 2014
1 parent 66c7087 commit 18c4b7d
Showing 1 changed file with 185 additions and 72 deletions.
257 changes: 185 additions & 72 deletions pip2/source/Assignments/ps6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Week 6: ends October 12
* Before Thursday's class:
* Read :ref:`Tuples<tuples_chap>`, and do the exercises in that chapter
* Read :ref:`Nested Data Structures and Nested Iteration<nested_chap>`, and do the exercises in that chapter
* Read :ref:`Installing a Native Python Interpreter and Text Editor <install_python_chap>` and follow the instructions to set up for running python on your computer
* Read :ref:`Installing a Native Python Interpreter and Text Editor <next_steps>` and follow the instructions to set up for running python on your computer


#. Reading responses
Expand All @@ -49,7 +49,7 @@ Reading Response

Suppose you write and edit a long text file over the course of several days, saving a new version every 15 minutes or so (``myfile1.txt``, ``myfile2.txt``, ``myfile3.txt``,...). Eventually, you have 100 different versions of the file. Now consider the whole directory containing all 100 versions of the file. Would it have a lot of redundancy? As a compression technique, how might you take advantage of the unix diff command in order to reduce the total amount of space required to store all 100 versions of the file?

.. activecode:: rr_7_2
.. activecode:: rr_7_1

# Fill in your response in between the triple quotes
s = """
Expand All @@ -59,7 +59,7 @@ Suppose you write and edit a long text file over the course of several days, sav



If you were to compute information entropy scores for all the students you've met since enrolling at the University of Michigan, which of them has the highest entropy and why?
Think about assigning entropy scores to people instead of documents. If you were to compute information entropy scores for all the students you've met since enrolling at the University of Michigan, which of them has the highest entropy and why?

.. activecode:: rr_7_2

Expand All @@ -81,7 +81,7 @@ Turn these in as screenshots via CTools in the Assignments tab!

#. In your ``new_program.py`` file, write the following code (copy it from here).

..activecode:: example_code_ps6
.. activecode:: example_code_ps6

def cool_machine(x):
y = x**2 +7
Expand All @@ -92,7 +92,7 @@ Turn these in as screenshots via CTools in the Assignments tab!

Then, run the Python program in your native Python interpreter. You should get an error. Take a screenshot of this and upload it to CTools.

Make edits to this code so it will work, without an error, and then save it with a different name (``fixed_program.py``). Now, run unix ``diff`` on these two files. Take a screenshot of the output, and upload it to CTools.
Make edits to this code so it will work (the only output should be 136.3), without an error, and then save it with a different name (``fixed_program.py``). Now, run unix ``diff`` on these two files. Take a screenshot of the output, and upload it to CTools.


Problem Set
Expand All @@ -112,17 +112,17 @@ You can see the function definition in the code below, but that's only so you ca
.. activecode:: ps_6_1

def give_greeting(greet_word="Hello",name="SI106",num_exclam=3):
final_string = greet_word + ", " + name + " " + "!"*num_exclam
final_string = greet_word + ", " + name + "!"*num_exclam
return final_string
#### DO NOT change the function definition above this line (only comments are OK)

# Write your three function calls below


#. Define a function called mult_both whose input is two integers, whose default parameter values are the integers 3 and 4, and whose return value is the two input integers multiplied together.
2. Define a function called mult_both whose input is two integers, whose default parameter values are the integers 3 and 4, and whose return value is the two input integers multiplied together.

.. activecode:: ps_6_2:
.. activecode:: ps_6_2

# Write your code here

Expand All @@ -136,97 +136,118 @@ You can see the function definition in the code below, but that's only so you ca



#. Print the second element of each tuple in the list ``new_tuple_list``.
3. Print the second element of each tuple in the list ``new_tuple_list``.

.. activecode:: ps_6_3

new_tuple_list = [(1,2),(4, "umbrella"),("chair","hello"),("soda",56.2)]


#. You can get data from Facebook that has nested structures which represent posts, or users, or various other types of things on Facebook. We won't put any of our actual Facebook group data on this textbook, because it's publicly available on the internet, but here's a structure that is almost exactly the same as the real thing, with fake data.
4. You can get data from Facebook that has nested structures which represent posts, or users, or various other types of things on Facebook. We won't put any of our actual Facebook group data on this textbook, because it's publicly available on the internet, but here's a structure that is almost exactly the same as the real thing, with fake data.

Notice that the stuff in the variable ``fb_data`` is basically a big nested dictionary, with dictionaries and lists, strings and integers, inside it as keys and values. (Later in the course we'll learn how to get this kind of thing directly FROM facebook, and then it will be a bit more complicated and have real information from our Facebook group.)

Follow the directions in the comments!

.. activecode:: ps_6_4

# first, look through the data structure saved in the variable fb_stuff to get a sense for it.

fb_stuff = {
"data": [
{
"id": "2253324325325123432madeup",
"from": {
"id": "23243152523425madeup",
"name": "Jane Smith"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "This problem might use the accumulation pattern, like many problems do",
"type": "status",
"created_time": "2014-10-03T02:07:19+0000",
"updated_time": "2014-10-03T02:07:19+0000"
},

{
"id": "2359739457974250975madeup",
"from": {
"id": "4363684063madeup",
"name": "John Smythe"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "Here is a fun link about programming",
"type": "status",
"created_time": "2014-10-02T20:12:28+0000",
"updated_time": "2014-10-02T20:12:28+0000"
}]
}

# Here are some questions to help you. You don't need to comment answers to these, but we suggest doing so! They may help you think through this big nested data structure.
# first, look through the data structure saved in the variable fb_data to get a sense for it.

fb_data = {
"data": [
{
"id": "2253324325325123432madeup",
"from": {
"id": "23243152523425madeup",
"name": "Jane Smith"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "This problem might use the accumulation pattern, like many problems do",
"type": "status",
"created_time": "2014-10-03T02:07:19+0000",
"updated_time": "2014-10-03T02:07:19+0000"
},

{
"id": "2359739457974250975madeup",
"from": {
"id": "4363684063madeup",
"name": "John Smythe"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "Here is a fun link about programming",
"type": "status",
"created_time": "2014-10-02T20:12:28+0000",
"updated_time": "2014-10-02T20:12:28+0000"
}]
}

# Here are some questions to help you. You don't need to
# comment answers to these (we won't grade your answers)
# but we suggest doing so! They
# may help you think through this big nested data structure.

# What type is the structure saved in the variable fb_data?
# What type does the expression fb_data["data"] evaluate to?
# What about fb_data["data"][1]?
# What about fb_data["data"][0]["from"]?
# What about fb_data["data"][0]["id"]?

# Now write a line of code to assign the value of the first message in the big fb_data data structure to a variable called first_message. Do not hard code your answer! (Write it in terms of fb_data.)
# Now write a line of code to assign the value of the first
# message ("This problem might...") in the big fb_data data
# structure to a variable called first_message. Do not hard code your answer!
# (That is, write it in terms of fb_data, so that it would work
# with any content stored in the variable fb_data that has
# the same structure as that of the fb_data we gave you.)


====

import test
print "testing whether variable first_message was set correctly"
test.testEqual(first_message,fb_data["data"][0]["message"])


#. Here's a warm up exercise on defining and calling a function:
5. Here's a warm up exercise on defining and calling a function:

.. activecode:: ps_6_5

# Define a function is_prefix that takes two strings and returns True if the
# first one is a prefix of the second one, False otherwise.
# Define a function is_prefix that takes two strings and returns
# True if the first one is a prefix of the second one,
# False otherwise.



# Here's a couple example function calls, printing the return value to show you what it is.
# Here's a couple example function calls, printing the return value
# to show you what it is.
print is_prefix("He","Hello") # should print True
print is_prefix("Hi","Hello") # should print False

====

import test
print 'testing whether "Big" is a prefix of "Bigger"'
test.testEqual(is_prefix("Big", "Bigger"), True)
print 'testing whether "Bigger" is a prefix of "Big"'
test.testEqual(is_prefix("Bigger", "Big"), False)



#. Now, 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...
6. Now, 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...

.. image:: Figures/HangmanSample.JPG

Expand All @@ -242,11 +263,20 @@ The first task you have to build part of the Hangman game follows:
# the original word, but with the unrevealed characters replaced by _

# a sample call to this function:
print(blanked("Hello", "el"))
print(blanked("hello", "elj"))
#should output _ell_

====

import test
print "testing blanking of hello when e,l, and j have been guessed"
test.testEqual(blanked("hello", "elj"), "_ell_")
print "testing blanking of hello when nothing has been guessed"
test.testEqual(blanked("hello", ""), "_____")
print "testing blanking of ground when r and n have been guessed"
test.testEqual(blanked("ground", "rn"), "_r__n_")

#. The second task to build part of the Hangman game:
7. The second task to build part of the Hangman game:

.. activecode:: ps_6_7

Expand All @@ -265,17 +295,47 @@ The first task you have to build part of the Hangman game follows:
#this should produce the output
#health: ----

====

import test
print "testing health_prompt(3, 7)"
test.testEqual(health_prompt(3,7), "+++----")
print "testing health_prompt(0, 4)"
test.testEqual(health_prompt(0, 4), "----")

#. Here's almost all of the Hangman game code. Paste your definition of the functions ``blanked`` and ``health_prompt`` at the start of this code. Then you can play when you run the program..!
8. Here's a function, game_state_prompt, that produces a prompt, suitable for display to a human player, telling the current state of the game. It includes calls to blanked() and health_prompt(). Copy your versions of those function in. Your task here is to correctly fill in the invocations of the function so that it returns the correct prompt strings.

.. activecode:: ps_6_8

# Here's where you should paste your function definitions.
def game_state_prompt(txt ="Nothing", h = 6, m_h = 6, word = "HELLO", guesses = ""):
res = "\n" + txt + "\n"
res = res + health_prompt(h, m_h) + "\n"
if guesses != "":
res = res + "Guesses so far: " + guesses.upper() + "\n"
else:
res = res + "No guesses so far" + "\n"
res = res + "Word: " + blanked(word, guesses) + "\n"

return(res)

p1 = game_state_prompt() # fill in parameters; see test results for correct output
p2 = game_state_prompt() # fill in parameters; see test results for correct output
p3 = game_state_prompt() # fill in parameters; see test results for correct output

====

import test
test.testEqual(p1, game_state_prompt("You already guessed that", 5, 6, "EASY", "ST"))
test.testEqual(p2, game_state_prompt("Yes, that letter is in the word", 2, 4, "EASY", "AST"))
test.testEqual(p3, game_state_prompt("Congratlations", 1, 6, "EASY", "EASTY"))

9. Here's almost all of the Hangman game code. You'll have to copy your definitions of blanked() and health_prompt() again. Then, the only thing you have to do is change the line that calls game_state_prompt, to provide the appropriate parameters. (Hint: all of the correct parameters are variables that are already set elsewhere in the program).
Then you can play when you run the program and play hangman!

def game_state_prompt(txt, h, m_h, word, guesses):
res = txt + "\n"
.. activecode:: ps_6_9

def game_state_prompt(txt ="Nothing", h = 6, m_h = 6, word = "HELLO", guesses = ""):
res = "\n" + txt + "\n"
res = res + health_prompt(h, m_h) + "\n"
if guesses != "":
res = res + "Guesses so far: " + guesses.upper() + "\n"
Expand All @@ -297,8 +357,8 @@ The first task you have to build part of the Hangman game follows:

# Now interactively ask the user to guess
while not game_over:
# replace this comment with code that invokes game_state_prompt and assign the return value to the variable prompt
prompt = game_state_prompt(feedback, health, max_health, secret_word, guesses_so_far)
# replace the next line with a correct invocation of game_state_prompt
prompt = game_state_prompt()
next_guess = raw_input(prompt)
next_guess = next_guess.upper()
feedback = ""
Expand Down Expand Up @@ -328,11 +388,64 @@ The first task you have to build part of the Hangman game follows:
sys.setExecutionLimit(60000) # let the game take up to a minute, 60 * 1000 milliseconds
main()

10. Look at the code for the Hangman game, repeated below. Then look at the flow chart. Write which lines of code go with which lines of the flow chart box, by answering the questions in comments below.

#. Look at the code for the Hangman game, below. (This code does NOT have your functions in it, so all the line numbers will be the same.) Then look at the flow chart. Write which lines of code go with which lines of the flow chart box, by answering the questions in comments below.
.. activecode:: ps_6_10

.. activecode:: ps_6_9
def game_state_prompt(txt ="Nothing", h = 6, m_h = 6, word = "HELLO", guesses = ""):
res = "\n" + txt + "\n"
res = res + health_prompt(h, m_h) + "\n"
if guesses != "":
res = res + "Guesses so far: " + guesses.upper() + "\n"
else:
res = res + "No guesses so far" + "\n"
res = res + "Word: " + blanked(word, guesses) + "\n"

return(res)

def main():
max_health = 3
health = max_health
secret_word = raw_input("What's the word to guess? (Don't let the player see it!)")
secret_word = secret_word.upper() # everything in all capitals to avoid confusion
guesses_so_far = ""
game_over = False

feedback = "let's get started"

# Now interactively ask the user to guess
while not game_over:
# replace the next line with a correct invocation of game_state_prompt
prompt = game_state_prompt()
next_guess = raw_input(prompt)
next_guess = next_guess.upper()
feedback = ""
if len(next_guess) != 1:
feedback = "I only understand single letter guesses. Please try again."
elif next_guess in guesses_so_far:
feedback = "You already guessed that"
else:
guesses_so_far = guesses_so_far + next_guess
if next_guess in secret_word:
if blanked(secret_word, guesses_so_far) == secret_word:
feedback = "Congratulations"
game_over = True
else:
feedback = "Yes, that letter is in the word"
else: # next_guess is not in the word secret_word
feedback = "Sorry, " + next_guess + " is not in the word."
health = health - 1
if health <= 0:
feedback = " Waah, waah, waah. Game over."
game_over= True

print(feedback)
print("The word was..." + secret_word)

import sys #don't worry about this line; you'll understand it next week
sys.setExecutionLimit(60000) # let the game take up to a minute, 60 * 1000 milliseconds
main()

# What line(s) of code do what's mentioned in box 1?

# What line(s) of code do what's mentioned in box 2?
Expand Down

0 comments on commit 18c4b7d

Please sign in to comment.