A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 2 years ago
week 2. Optional quest.2Secret Messages
('create a cyclic cipher')
Has anybody done this, as i'm stumped!
 2 years ago
week 2. Optional quest.2Secret Messages ('create a cyclic cipher') Has anybody done this, as i'm stumped!

This Question is Closed

algotruneman
 2 years ago
Best ResponseYou've already chosen the best response.0See if this makes sense. c = '' b = '' a = 'abcdefghijklmnopqrstuvwxyz' shift = 5 for i in range(0,len(a)shift): b = b + a[i] for i in range(len(a)shift,len(a)): c = c + a[i] b = c+b print 'ORIGINAL' print a print 'SHIFT',shift print b

kenray12
 2 years ago
Best ResponseYou've already chosen the best response.2I don't want to give away my answer (you learn better when you get that critical flash of insight yourself), but: 1. I did find it useful to consult the ASCII table to see the limits of the lower case and upper case characters  these are the two groups of characters you have to check against. 2. Since they are encouraging the use of the mod operator (%), I found it easiest to offset the original character from its ASCII limits to a 126 range to be able to use the mod operator correctly. Of course, you will then need to "deoffset" the character after you shift it to get a reasonable result. 3. My conditional loop itself is seven lines long with three checks, so it doesn't have to be long or complicated to work right. (I usually find that I have found the optimal solution to a programming task when my code is concise.) It is easy (for me) to get confused when dealing with programming constructs like ord(c) vs c itself when there are several manipulations of data, as in this project, so I find it useful to print the value of every variable when troubleshooting and to use short test strings to see how I'm coming along. Hope this is at least a bit helpful.

petew71
 2 years ago
Best ResponseYou've already chosen the best response.0Thanks for the prompt responses! @algotruneman ... yes, i do see what your doing there. Different from my attempted approach ! @kenray12 ... I had an outline of the basic stuff. But the creative math logic of the modulus to account for the offset is beyond me. My learning has always been (and continues to be..) a slow, gradual slog. Flashes of inspiration are pretty much alien to my brain.. This is where i got so far. ( Constructive comments?..Any more mod hints?) def rotate(phrase,move): encoded_phrase = " " for letter in (phrase): if letter.isalpha(): if letter.isupper(): ascii_code = ord(letter) ascii_code += move elif letter.islower(): ascii_code = ord(letter) ascii_code += move encoded_phrase += chr(ascii_code) print 'The encoded phrase is:',encoded_phrase phrase = raw_input ("Enter sentence: ") shift = raw_input("Enter shift: ") rotate(phrase,int(shift)) # Thanks for your time ...Pete

petew71
 2 years ago
Best ResponseYou've already chosen the best response.0Update: Well, after a bit of looking around on Google and experimenting with my program, i eventually got there! I have to say, i'd not have got that way of using modulo (combined with subtracting...) without some heavy hints !
Ask your own question
Ask a QuestionFind 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.