Due at 10am on 2/19/08 1. Call this program hw6.1.py.
Design and implement a program that lets the user navigate through a small choose-your-own-adventure story. (If you're feeling dull, you can implement Queneau's "A Story As You Like It", which is in the first Oulipo packet from Fall quarter.) The story should allow the user to make several different choices leading to different outcomes. There must be at least one loop in the story: that is, it must be possible to get from one place in the story back to that same place in the story by making some sequence of legal choices. (For example, in a choose-your-own-adventure book, you might be able to go from page 13 to page 39, then from page 39 to page 18, and from page 18 to page 13; that'd be a loop.)
Your program should represent each "page" of the story – each bit of story (which will probably be much shorter than a page in length) that leads to the next choice – as a separate function. Call those functions page1(), page2(), and so on. Each page function should (1) tell that bit of the story, (2) prompt the user for her/his choice, and (3) call the appropriate next page fuction to continue the story according to the user's selection. When the story reaches an end, the program should ask the user if s/he wants to go through it again. Your program needs to have a minimum of 10 "pages".
2. Call this program hw6.2.py.
In this exercise we'll make a simple text encoding/decoding program. A Caesar cipher or shift cipher is a simple way of encoding text; it works by shifting each letter of the alphabet by a certain number of characters. For example, in a Caesar cipher with a shift of 2, A becomes C, B becomes D, C becomes E, and so on, all the way down to the end of the alphabet, where X becomes Z. But how do we shift Y by 2? The only sensible choice is to wrap around to the beginning, so Y becomes A, and Z becomes B. So the text "BABYCAKES" becomes "DCDAECMGU" under a Caesar cipher with shift 2.
Write a function that takes a string and an integer and encodes the string by shifting each English letter by that number of places. The function should do nothing to characters other than letters. The function should ignore case – that is, it should shift both lowercase and uppercase letters. It's fine if the case of the output is different from the case of the input. (So it's fine if "bAbYcAkEs" comes out as "DCDAECMGU".)
Your program should take a string from the user and then print out the result of shifting that string by every number from 0 to 25. That is, for one string input, your program should produce 26 lines of output, showing the result of all possible shift values. For example, if the input were "abc", the output might look like this:
(To use this as a decoder, you can enter an encoded string and then look for a meaningful line in the output.)
To make the code wrap around at the end of the alphabet, you'll need to use the mod operator, which in Python is written as %, as well as the ord() and chr() functions discussed in last week's homework. Here's some code that you may want to use:
charnum = ord(char) - ord('a')
newcharnum = (charnum + shift) % 26
newchar = chr(newcharnum + ord('a'))
Here char is the original letter (before any shifting occurs), shift is the number of letters to shift by, and newchar is the shifted version of char. What's happening in this code is: first we find charnum, which tells us which place in the alphabet char occupies (0 through 25); then we find newcharnum, which tells us which place in the alphabet the shifted version of char occupies, using % to turn numbers bigger than 25 into numbers in the range 0-25; and then we translate newcharnum back into a character, newchar. (This probably won't make sense right away; put it into a function and play around with it to see what it actually does.)
3. (OPTIONAL) Call this program hw6.3.py.
It's fairly well-known that Tic-Tac-Toe results in a draw if both players play perfectly. Modify the Tic-Tac-Toe program from the book to make the program unbeatable.
4. (OPTIONAL) Call this program hw6.4.py.
Write a program that translates Roman numerals to the corresponding base 10 numerals. Unlike in lab 6, here we have to do it the right way; here IV is 4, not 6.
5. (OPTIONAL) Call this program hw6.5.py.
Write a program that translates base 10 numerals to the corresponding Roman numerals. Here again we have to do it the right way.
6. (OPTIONAL) Call this program hw6.6.py.
Write a program that takes a list and prints out all of the possible permutations of elements in the list. (Hint: using a recursive function can make this fairly straightforward.)