At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
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.
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 prior to adding to the ord of 'A'?
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.
Not the answer you are looking for? Search for more explanations.
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.
Most probably have this, but for those who do not: http://www.asciitable.com/
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
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
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.