## petew71 2 years ago week 2. Optional quest.2-Secret Messages ('create a cyclic cipher') Has anybody done this, as i'm stumped!

1. algotruneman

See 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

2. kenray12

I 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 1-26 range to be able to use the mod operator correctly. Of course, you will then need to "de-offset" 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.

3. petew71

Thanks 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

4. petew71

Update: 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 !