Once again, this week I will ask you to work with a partner of your choice. You will turn in a SINGLE answer sheet to the instructor or TA before you leave and submit ONE version of your code under one person's eLearning account before the next class period. As a reminder, this means:
Failure to follow these guidelines may cause you to lose points for this activity.
Over the last few days we took a look at some string methods. Let's take a look at a string method called find, which can be used to find the index of a particular character (or substring) inside of a string.
First, I would like you to get familiar with the built-in Python docs. Start IDLE, click on the Help menu, and select "Python Docs". You are now inside the Python language help documentation. Take a few minutes to click around. Once you are familiar with the layout of the Python docs, please find some information about the string method called find.
[Q1] Please write down the steps you used to find the string method called find in the python docs.(Note: If you can't find it after a few minutes, raise your hand to get a hint.)
Fun Fact: If you forget the name of a method while programming inside of IDLE, you can type the name of your string object followed by a "." and press the tab key.
IDLE also likes to give you hints on the types and numbers of arguments that you can give to any particular method. For example, if you type the name of your string, then a dot, then an open parenthesis, IDLE will show you a yellow-ish box with some helpful information.
At the command prompt type
>>> myStr = "">>> myStr.find(
Now let's actually play with the find string method. At the command prompt type
>>> myStr = "I hope it is a sunny day."
Command | Results |
myStr[0] | |
myStr.find("I") | |
myStr[4] | |
myStr.find("p") | |
myStr.find("i") | |
myStr.find("a") | |
myStr.find("a",14) | |
myStr.find("sunny") | |
myStr.find("cloudy") | |
myStr[myStr.find("I")] | |
myStr[myStr.find("I")+2] |
[Q4] What is happening when the find method returns a -1?
Introduction to Today's Lab Assignment -- A Cypher!
In this assignment, we are going to implement a simple encoding and decoding of
text. As a result of working on this project, you will gain more experience with the use of:
1. strings and methods
2. if statement
3. for loop and while loop
Background
A rotation cipher is one of the simplest, plain-text ciphers, known since at
least the time of Julius
Caesar. It takes in a plain-text string, and translates it into a new string
based on a rotation of the
alphabet being used. The basis is a "rotation", a re-sequencing of an alphabet.
Consider the following
example. Consider the alphabet being a single string consisting of the lower
case English letters as
below (shown with each letter's associated index):
then a rotation of 3 means that the first three letters of the alphabet are
moved to the end of the sequence, while the other letters move up, as shown
below. Notice that the movement is done one
letter at a time.
A cipher is created by using the rotated alphabet to replace each letter in the original string with its rotated equivalent letter. Using the example above, the word "this" is translated as follows:
Thus the string 'this' becomes the string 'wklv' using a rotation of 3.
Write a program that:
- 'e' to encode a string
- 'q' to quit
Any other command should raise an error and reprompt.
The program then returns the encoded string. Important, the program should not encode any letter that is not in the lower
case
alphabet. Those letters should simply be passed through to the encoded string
SUGGESTIONS/HINTS
There are two ways to make the encoding. The first way SEEMS easier but can be hard. The second way SEEMS hard but can be very easy once you see it. I would encourage you to try the second technique.
Now use slicing to generate the rotated string. This too is a 26 character string, but it starts with the character that "a" would "become" Think about how you can slice this into two pieces and then concatenate them back together in the new/different order. For example, if the rotation value is 3 then you would produce the string that is:
Given these two strings, one the lower-case alphabet (base) and one a rotated alphabet (rotated), how can you encode the secret message string? You need to go through each letter of the message string to encode it. To do this, find it in the base string, remember its location/index in that alphabet, then find the letter in the rotated alphabet at the same index.
For example, if I needed to encode the letter "h" I would find that it is in index 7 in the base string. Then I could find that letter "k" is at index 7 in the rotated string. Notice that "k" is three letters after "h" so this IS the correct encoding.
But how do you do that?
HINT: The python method find is helpful here. It indicates the location of a letter. Consider the string "hello":
I actually think that this later technique is harder to understand at first but EASIER to write in code.
Don't forget that you are only supposed to encode lower case letters. All other characters stay the same. When encoding a string, you should check to see if the letter from the original string is in the lower case alphabet. You may use the in operator. ('a' in 'abcde') returns True. ('A' in 'abcde') returns False.
Before you move on, test your code...
[SIG1] Please show this to an instructor BEFORE moving on
Add to your program so that it:
- 'e' to encode a string
- 'd' to decode the string using the rotation number
- 'q' to quit
For example:
[SIG2] Please show this to an instructor BEFORE moving on
At this point I would save a copy of your code. If you do not get Part C completed before the end of the lab you should submit this copy. You will get credit for Part A and B only. However, I want you to TRY Part C.
Add to your program so that it:
- 'e' to encode a string
- 'd' to decode the string using the rotation number
- 'c' to attempt to crack the code
- 'q' to quit
The program then needs to calculate which rotation value will mean that the single word would be in the encoded message and use this value to decode the secret message.
This probably sounds VERY hard. But in fact, it isn't that bad. You know that there are only 25 rotation values. Begin by encoding the plain text word with a rotation of 1. Then, check to see if that encoded word is in the larger encoded string.
As soon as you find the rotation value you can quit and print the results. If you check all 25 rotation values than there was an error and you should print an appropriate message.
A sample run might look like this:
Final Submission
This week I will again ask you to submit your code for electronic grading, using the eLearning submission system.
Follow the directions on the system to select the appropriate course and assignment and submit
Don't forget to hand in your answer sheet to the professor or TA before you leave!