Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

petew71

  • one year ago

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

  • This Question is Closed
  1. algotruneman
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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 !

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.