CS21 Lab3: Boolean and Strings

Due 11:59pm Tuesday, February 9

Run update21, if you haven't already, to create the cs21/labs/03 directory. handin21 looks for your lab 3 assignments in the cs21/labs/03 directory, so make sure you create your python programs in there:

$ cd cs21/labs/03
$ vim leapyear.py
$ gvim leapyear.py  # or you can use gvim

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

Leap year
[Problem 11 in Chapter 7 of Zelle text] A year is a leap year if the year is divisible by 4, but not divisible by 100. However a year that is divisible by 400 is a leap year. For details on why leap years have these funny 100 and 400 year exceptions, see a description from howstuffworks.

Write a program leapyear.py that prompts the user to enter a year and prints if the year entered is a leap year or not.

$ python leapyear.py
Enter a year: 2000
2000 is a leap year
$ python leapyear.py
Enter a year: 1900
1900 is not a leap year
$ python leapyear.py
Enter a year: 1984
1984 is a leap year
$ python leapyear.py
Enter a year: 2008
2008 is a leap year
$ python leapyear.py
Enter a year: 2010
2010 is not a leap year
$ python leapyear.py
Enter a year: 2003
2003 is not a leap year
Double letter

Write a program called doubleletter.py that prompts the user to enter a phrase and a letter. The program should create a new string that consists of the characters in the phrase where all occurrences of the letter are doubled in the new string.

$ python doubleletter.py
This program modifies a phrase by doubling all letters 
matching a letter specified by the user.

Enter a phrase: hello there
Which letter would you like to double? e
The new phrase is: heello theeree 

$ python doubleletter.py
This program modifies a phrase by doubling all letters 
matching a letter specified by the user.

Enter a phrase: bo ho
Which letter would you like to double? o
The new phrase is: boo hoo

Hint: Think accumulator pattern, and think if/else.
Cyclic Cipher

Write a program, in a file named encode.py, that asks the user to enter a phrase and a cyclic shift value, and produces an encoded string by applying the cyclic cipher method to just the lower-case alphabetic characters in the phrase. Your solution should encode ONLY lower case characters in the phrase. Several hints at the end of this document help guide you towards a solution. It is highly recommended that you follow the hints and test your program incrementally.

A cyclic cipher, with a shift value of 3 will encode the character 'a' as the character 'd', the character 'b' as 'e', and so on (each character's encoding is shifted 3 characters to the right). A cyclic cipher wraps the encoding around the alphabetic characters, so that in this example, 'w' is encoded as 'z', 'x' is encoded as 'a', 'y' is encoded as 'b', and 'z' is encoded as 'c'. You can think of this as encoding where the alphabet is arranged in a circle, and the shift amount tells you how many places in one direction to move to find the encoding of any letter of the alphabet.

Here is what a few runs of your program might look like:

$ python encode.py
This program encodes a phrase using a cyclic cypher
Enter a phrase: a b c x y z
Enter a shift value: 3
The encoded phrase is: d e f a b c

$ python encode.py
This program encodes a phrase using a cyclic cypher
Enter a phrase: a b c x y z
Enter a shift value: -3
The encoded phrase is: x y z u v w

$ python encode.py
This program encodes a phrase using a cyclic cypher
Enter a phrase: a b c x y z
Enter a shift value: 1000
The encoded phrase is: m n o j k l

$ python encode.py
This program encodes a phrase using a cyclic cypher
Enter a phrase: Hello there, what is happening?
Enter a shift value: 4
The encoded phrase is: Hipps xlivi, alex mw lettirmrk?
Notice in the sample output that only lower case characters in the phrase are being encoded (in the last example, the 'H', ',', and '?' characters are left unencoded in the result string).

Hints for solving: I'd suggest approaching this problem in the following way:

  1. First, write the program so that the user enters a phrase, and your program constructs a new string consisting of all the characters in the original phrase where every lower-case alphabetic character is replaced with the character 'x' (all other non-lower-case alphabetic characters stay the same in the new string). For example, if the user enters the phrase "To be, or not to be!" your program will produce a new string "Tx xx, xx xxx xx xx!" (notice the 'T', ',', and '!' are not replaced with x's).

  2. Once that works, next see if you can produce an encoded string where the lower-case alphabetic characters are encoded by doing only the shift part of the cyclic cipher (don't do the cyclic, wrap-around, part of the encoding yet). For example, a shift value of 4 will encode 'a' as 'e', 'b' as 'f', ..., 'u' as 'y', 'v' as 'z', 'w' as whatever the character after 'z' is in the ascii encoding, 'x' as whatever the character is two beyond 'z' in the ascii encoding, ...

  3. Once that works, add in code to do the cyclic part of the shift. For example, a shift value of 4 will encode 'a' as 'e', 'b' as 'f', ..., 'v' as 'z', 'w' as 'a', 'x' as 'b', ...

    You will need to use the mod operator % to do a cyclic shift. Here is an example of a similar problem. The days of the week are cyclical. Suppose we assign the following numeric code to the days of the week:

    0 Sunday
    1 Monday
    2 Tuesday
    3 Wednesday
    4 Thursday
    5 Friday
    6 Saturday
    
    If we wish to determine what day of the week is 100 days from Tuesday, we can look at the code for Tuesday (2), add 100, and mod the result by 7. Python tells us that (2+100)%7=4 meaning 100 days from Tuesday is a Thursday. Try a few more examples. This example works well because our numeric code started at 0 for Sunday incremented by one each time until Saturday. The expression x%y will always return a number in the range 0 to y-1 inclusive. Suppose we shifted our numeric code by 3 spots yielding the following:
    3 Sunday
    4 Monday
    5 Tuesday
    6 Wednesday
    7 Thursday
    8 Friday
    9 Saturday
    
    Now (5+100)%7=0, so using mod directly on this code doesn't even give us a valid code number for a day of the week. Think about this example as you try to implement your cyclic cipher.
Extra Challenge Problem
Do not try this before completing the required lab 3 problems.
This is NOT a required part of the lab 3 assignment.

If you would like an extra challenge you can try this problem: extra challenge problem

Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.