A community for students.
Here's the question you clicked on:
 0 viewing
TomDeBeauchamp
 2 years ago
MIT 6.189 First Week  Exercise OPT.2 – Secret Messages
in Section 3, What are we being asked to do? I understand the modulus and how it works, but I am not sure what the problem is asking.
TomDeBeauchamp
 2 years ago
MIT 6.189 First Week  Exercise OPT.2 – Secret Messages in Section 3, What are we being asked to do? I understand the modulus and how it works, but I am not sure what the problem is asking.

This Question is Closed

TomDeBeauchamp
 2 years ago
Best ResponseYou've already chosen the best response.0In the problem itself it says: "We want the values of the numbers to remain between 0 and 5. To do this we will use the modulus operator. The expression x%y will return a number in the range 0 to y1 inclusive, e.g. 4%6 = 4, 6%6 = 0, 7%6 =1. " I get we might want to keep the numbers below a certain threshold, but how is the modulus helpful? And how can we be sure that we can get back to the original phrase? I will point out that I don't know much about ASCII, or any other encodings.

whpalmer4
 2 years ago
Best ResponseYou've already chosen the best response.0All you need to know about ASCII for this is that the letters are assigned to consecutive values. So the value of ord('A') + 3 = ord('D'), just as you would expect. If you wanted to shift a character by 4, you would add 4 to the value returned by ord, but you need to keep that from going beyond the end of the alphabet. What if you took the position of the letter in the alphabet, added the shift, and then took the modulus <length of alphabet> prior to adding to the ord of 'A'?

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.0Lo Tom, I saw you took a look at the way I did it. The version I did was a bit quick and dirty. If you look at the range of characters, you could make a way to shift only lower case to lower case, upper to upper, and so on. I just took a bit of a shortcut. To be honest, if I wanted it clean I would use an if statement with a regular expression to find capitol letters, and shift them only with the range of other capitols. Etc.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.0for i in range(32,127): print(str(i)+" is \""+ chr(i)+'"') For 2.x, remove the ( ) from the print. That will show you the character range(s) you are interested in.

eSpeX
 2 years ago
Best ResponseYou've already chosen the best response.0Most probably have this, but for those who do not: http://www.asciitable.com/

TomDeBeauchamp
 2 years ago
Best ResponseYou've already chosen the best response.0I think my problem has been understanding the question itself. I'd gotten this far: phrase = raw_input("Type the phrase you'd like to see encoded. ") shift = int(raw_input("Type the shift value. ")) encoded_phrase = '' for letter in phrase: ascii_code = ord(letter) + shift letter_res=chr(ascii_code) encoded_phrase += letter_res print "The encoded phrase is: " + encoded_phrase This does encode the phrase, but it uses a character set far larger than what we're after. The problem wants us to go from AZ, starting over again at A. whpalmer  Thanks for your note, I get how the modulus is important to this operation. "T" shifted 7 should be "A". 20+7 == 27. 27%26 == 1

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.0A better way would be to take things in as numbers, toss them into a matrix, ans then use an encoding matrix to garble the numbers. The numbers could then be shipped in matrix or vector form to be rebuilt on the other end, and the inverse matrix would decode them. The process of matrix multiplication would hide common values, like the letter e.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.