Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 15 Online
OpenStudy (anonymous):

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.

OpenStudy (anonymous):

In 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 y-1 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.

OpenStudy (whpalmer4):

All 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'?

OpenStudy (e.mccormick):

Lo 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.

OpenStudy (e.mccormick):

for 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.

OpenStudy (espex):

Most probably have this, but for those who do not: http://www.asciitable.com/

OpenStudy (anonymous):

I 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 A-Z, 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

OpenStudy (e.mccormick):

A 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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!