CS 30 Homework 2

Due by class time Wednesday, February 8

If you wish, you may work with a partner on this assignment.

Reading

Practice Exercises (Optional)

These will give you extra practice with Python, but you do not need to turn them in. I would strongly recommend doing them if you have never programmed before.

Problems to Turn In

The following problems should be turned in. Using IDLE, create a new file named assign2.py and put all of your program definitions in this file. That way, you can load them all into Python at once for testing by simply pressing the [F5] key.

  1. Finish the problems from Lab 3. Name your programs as follows:

  2. Problem 8 on page 119: Caesar ciphers. You should define two programs, one for encoding messages called encode() and the other for decoding messages, called decode(). The key value used to encode or decode a message should be kept small, in order to avoid problems (see the Extra Credit below). For example:

    >>> encode()
    Enter a message: Sourpuss
    Enter a key: 2
    The encoded message is:
    Uqwtrwuu
    >>> decode()
    Enter a message: Uqwtrwuu
    Enter a key: -2
    The decoded message is:
    Sourpuss
    
  3. Problem 12 on page 120: string formatting. Call your program chaos(). Try to make your program's output look exactly like the output shown in the book.

EXTRA CREDIT

  1. Problem 9 on page 119. Hint: Python's remainder operator % will come in handy here. For example, if N is some value in the range 0-9 and we add 5 to N, the resulting sum might exceed 9. We can force the sum to stay in the range 0-9 by applying the % operator as follows: (N + 5) % 10.

  2. Problem 7 on page 119. Call your program fullnameValue(). Hint: Reuse the code you wrote for problem 6, which computes the numeric value of an individual name, but embed it within an outer for-loop, which cycles through each name in the full name entered by the user. In other words, you'll have a for-loop within a for-loop!

Turning in Your Homework

Put all of your function definitions into a single Python file called assign2.py and include your name in a comment (i.e. a line beginning with #) at the top of the file. Submit this file electronically by running the /common/cs/submit/cs30-submit script from any Pomona Linux machine. Be sure to run this command from the directory containing your file.

If you have questions about anything, don't hesitate to ask!