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 $ gvim leapyear.py
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
Linguistic vowel shifting can cause some words to be pronounced differently in various dialects. We can get a very, very rough approximation of how things might change by swapping vowels in a phrase with alternative vowels.
Write a program called swapletter.py that prompts the user to enter a phrase and two letters. The program should then swap all occurrences of the first letter in the phrase by the second letter.
$ python swapletter.py Enter a phrase: potato buritto Which letter would you like to change? o Which letter would you like to replace it with? a The new phrase is: patata buritta $ python swapletter.py Enter a phrase: bag tag Which letter would you like to change? a Which letter would you like to replace it with? e The new phrase is: beg teg $ python swapletter.py Enter a phrase: butter batter Which letter would you like to change? t Which letter would you like to replace it with? d The new phrase is: budder badderHint: 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 alphabetic characters in the phrase. Your solution should encode ONLY upper and 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 cipher Enter a phrase: Hello there, Zoe! What is happening??? Enter a shift value: 4 The encoded phrase is: Lipps xlivi, Dsi! Alex mw lettirmrk??? $ python encode.py This program encodes a phrase using a cyclic cipher Enter a phrase: Lipps xlivi, Dsi! Alex mw lettirmrk??? Enter a shift value: -4 The encoded phrase is: Hello there, Zoe! What is happening??? $ python encode.py This program encodes a phrase using a cyclic cipher Enter a phrase: yee ha!!! Enter a shift value: 1000 The encoded phrase is: kqq tm!!!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.
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.