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.
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
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 hooHint: Think accumulator pattern, and think if/else.
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:
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 SaturdayIf 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 SaturdayNow (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.
If you would like an extra challenge you can try this problem: extra challenge problem
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.