CS21 Lab 4: while loops and functions

Due Saturday (October 3) before midnight

This lab assignment requires you to write some Python programs. First, run update21 to get the template files we have provided for you. These will serve as starting points for your programs. Next, use cd to change into your cs21/labs/04 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$update21$ cd cs21/labs/04
$pwd /home/your_user_name/cs21/labs/04  We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory! Programming tips As your programs become larger, it is even more important to develop good habits: • Use a comment at the top of the file to describe the purpose of the program. • Use variable names that describe the contents of the variables. • Use def main() to combine the steps of your program into a function. The above are mandatory and you will be graded on them. We strongly advise you to write your programs incrementally and test them as you go. No one writes interesting programs all at once. As you write, test that your program does what you expect (even if it doesn't yet do what you want). If it surprises you, make sure you understand what's happening before writing more code. We have provided some test input and output; make sure you test your program! You should also test your program using some inputs and outputs that were not shown here; after all, we will. Come up with your own test cases and verify that the program is producing the right output on them. 1. Nim Source: OpenClipArt.org You will write a computer program which allows the user to play Nim, an ancient game with numerous variants, a global history, and interesting connections to mathematics. Nim is a strategy game based on subtraction. In our variant, there are two players and three rows of sticks. At the beginning of the game, the first row contains three sticks, the second row contains five sticks, and the third row contains seven sticks. Players alternate, taking sticks from one of the rows; players may take any number of sticks they please, but must take at least one and may only take from one row per turn. The objective is to force the other player to take the last stick. Your computer program will allow two players to play a hotseat version of the game -- both players play the game by taking turns using the program. Your program will keep track of whose turn it is and how many sticks are left in each pile. Because multiple people are using the program, each prompt should indicate whose turn it is. You have been provided a template file, nim.py. This file contains a number of functions which you are to use in writing your implementation of Nim. You simply need to write the body of the main function which plays the game. Here are some sample runs of the program. $ python nim.py
1: |||
2: |||||
3: |||||||
Player 1: which row? 2
Player 1: take how many? (max 5) 5
1: |||
2:
3: |||||||
Player 2: which row? 3
Player 2: take how many? (max 7) 6
1: |||
2:
3: |
Player 1: which row? 1
Player 1: take how many? (max 3) 3
1:
2:
3: |
Player 2: which row? 3
Player 2: take how many? (max 1) 1
Player 1 wins!
$python nim.py 1: ||| 2: ||||| 3: ||||||| Player 1: which row? 0 That is not a valid row. Player 1: which row? 1 Player 1: take how many? (max 3) 3 1: 2: ||||| 3: ||||||| Player 2: which row? 1 That is not a valid row. Player 2: which row? 3 Player 2: take how many? (max 7) 6 1: 2: ||||| 3: | Player 1: which row? 2 Player 1: take how many? (max 5) 3 1: 2: || 3: | Player 2: which row? 2 Player 2: take how many? (max 2) 2 1: 2: 3: | Player 1: which row? 3 Player 1: take how many? (max 1) 4 That is not a valid number of sticks. Player 1: take how many? (max 1) 1 Player 2 wins!  2. Coded Messages Source: Wikipedia.org Last week's lab included a program which was required to extract a secret message from a large string. This week, you will write a program which can transform a string in several ways. A sequence of these operations can be used to create an encoded message; performing the opposite steps in reverse will recover the original message. Your program will start by requesting a message from the user. Then, you will accept a series of commands: left, right, flip, dosido, and swap. Each of those commands change the message in some way: • left moves every character in the string to the left. The leftmost character wraps around to the right. For instance, "fish" becomes "ishf". right does the opposite, moving every character to the right. • flip reverses the string: the first character becomes the last, the second character becomes next-to-last, and so on. For instance, "fish" becomes "hsif". • dosido swaps each pair of characters: the first and second characters trade places, the third and fourth characters trade places, and so on. If the string is of odd length, the last character stays where it is. For instance, "computer" becomes "ocpmture" and "mouse" becomes "omsue". • When the user commands you to swap, you will ask for two letters. Then, each occurrence of the first letter is replaced with the second letter and vice versa. For instance, if the user asks you to swap the letters l and e, then "hleeo" becomes "hello". Make sure to run update21 so that you get the template file, code.py, that we have provided for you. This template file contains a number of functions, all of which have the pass statement in their bodies. The pass statement is a "do-nothing" statement that technically satisfies the requirement that every function has a body. This way, you can write the code for one function at a time and test your progress without having to write everything at once. This part of the lab is, in a way, the opposite of the Nim problem: we have given you (most of) a main function and you must now fill in the functions that it is using! Note that you must also update the main function, as it presently only understands the left and right commands. If you write your functions correctly, though, you shouldn't have to change anything that already appears in main. Here's a sample run of this program: $ python code.py
What is your message? It's easier to ask forgiveness than it is to get permission.
What would you like to do? left
Your message is now: t's easier to ask forgiveness than it is to get permission.I
What would you like to do? flip
Your message is now: I.noissimrep teg ot si ti naht ssenevigrof ksa ot reisae s't
What would you like to do? dosido
Your message is now: .Ionsiisrmpet geo  tist  ianths esenivrgfok aso  tersieas t'
What would you like to do? swap
What is the first letter to swap? a
What is the second letter to swap? e
Your message is now: .Ionsiisrmpat gao  tist  ienths asanivrgfok eso  tarsiaes t'
What would you like to do? scramble
I don't know the action "scramble".
Your message is now: .Ionsiisrmpat gao  tist  ienths asanivrgfok eso  tarsiaes t'
What would you like to do? swap
What is the first letter to swap? e
What is the second letter to swap? a
Your message is now: .Ionsiisrmpet geo  tist  ianths esenivrgfok aso  tersieas t'
What would you like to do? dosido
Your message is now: I.noissimrep teg ot si ti naht ssenevigrof ksa ot reisae s't
What would you like to do? flip
Your message is now: t's easier to ask forgiveness than it is to get permission.I
What would you like to do? right
Your message is now: It's easier to ask forgiveness than it is to get permission.
What would you like to do? quit

3. The Tortoise and the Hare

Source: Wikipedia.org

Aesop's fable of the Tortoise and the Hare features a quick but overconfident hare in a race against a tortoise. Your task is to simulate the race between the tortoise and the hare. Your program will ask the user for the length of the race (which you may assume is a positive integer) and then simulate it under the following rules.

• We will check in on the race once every minute.
• Each minute, the tortoise moves one meter.
• Each minute, there is a 20% chance that the hare moves four meters. The other 80% of the time the hare was napping and didn't move at all.

The race ends when one or both of the participants has traveled at least as far as the length of the race given by the user. The winner is the participant who traveled the furthest; if both participants traveled the same distance, the race is a tie.

After each minute of the race, you must print a visual status report of the race. This status report must consist of three lines. The first line must show the distance the hare has traveled (one character per meter) with asterisks behind traveled spaces. The second line shows the tortoise. The third line is a series of dashes equal to the length of the race, with an extra character in front to represent the starting line. We show the tortoise with a capital T and the hare with a capital H. You must write a function to show the status of the race; the function header is included in the template file, race.py, for you.

Below are some sample executions of this program.

$python race.py How long should the race be? 8 H T |-------- ****H *T |-------- ****H **T |-------- ****H ***T |-------- ****H ****T |-------- ********H *****T |-------- The hare wins!$ python race.py
How long should the race be? 20
H
T
|--------------------
H
*T
|--------------------
H
**T
|--------------------
H
***T
|--------------------
H
****T
|--------------------
****H
*****T
|--------------------
****H
******T
|--------------------
****H
*******T
|--------------------
****H
********T
|--------------------
****H
*********T
|--------------------
****H
**********T
|--------------------
********H
***********T
|--------------------
************H
************T
|--------------------
************H
*************T
|--------------------
************H
**************T
|--------------------
************H
***************T
|--------------------
************H
****************T
|--------------------
************H
*****************T
|--------------------
************H
******************T
|--------------------
************H
*******************T
|--------------------
************H
********************T
|--------------------
The tortoise wins!


Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.