DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

How to implement Caesar cipher [duplicate]

StackOverflow https://stackoverflow.com/questions/13216749
  •   python
  •  | 
  •  
  •  | 
  •   ( words)

Question

Possible Duplicate:
Caesar’s Cipher using python, could use a little help

Alright, so in my class, I'm supposed to write code that shifts a dictionary as a Ceasar cipher. My code shifts everything fine, but returns the keys in a different order than the provided test case.

My code:

sLowerCase = string.ascii_lowercase
sUpperCase = string.ascii_uppercase
dCode = {'A':'A', 'B':'B', 'C':'C', 'D':'D', 'E':'E', 'F':'F', 'G':'G', 'H':'H', 'I':'I', 'J':'J', 'K':'K', 'L':'L', 'M':'M', 'N':'N', 'O':'O', 'P':'P', 'Q':'Q', 'R':'R', 'S':'S', 'T':'T', 'U':'U', 'V':'V', 'W':'W', 'X':'X', 'Y':'Y', 'Z':'Z',
         'a':'a', 'b':'b', 'c':'c', 'd':'d', 'e':'e', 'f':'f', 'g':'g', 'h':'h', 'i':'i', 'j':'j', 'k':'k', 'l':'l', 'm':'m', 'n':'n', 'o':'o', 'p':'p', 'q':'q', 'r':'r', 's':'s', 't':'t', 'u':'u', 'v':'v', 'w':'w', 'x':'x', 'y':'y', 'z':'z'}

for c in dCode.keys():

    if c in sUpperCase:

        if sUpperCase.index(c) + shift > 25:

            dCode[c] = sUpperCase[(sUpperCase.index(c) + shift) - 26]

        else:

            dCode[c] = sUpperCase[(sUpperCase.index(c) + shift)]

    if c in sLowerCase:

        if sLowerCase.index(c) + shift > 25:

            dCode[c] = sLowerCase[(sLowerCase.index(c) + shift) - 26]

        else:

            dCode[c] = sLowerCase[(sLowerCase.index(c) + shift)]

return dCode

My output for buildCoder(0) (easiest to read obviously)

{'B': 'B', 'D': 'D', 'F': 'F', 'H': 'H', 'J': 'J', 'L': 'L', 'N': 'N', 'P': 'P', 'R': 'R', 'T': 'T', 'V': 'V', 'X': 'X', 'Z': 'Z', 'b': 'b', 'd': 'd', 'f': 'f', 'h': 'h', 'j': 'j', 'l': 'l', 'n': 'n', 'p': 'p', 'r': 'r', 't': 't', 'v': 'v', 'x': 'x', 'z': 'z', 'A': 'A', 'C': 'C', 'E': 'E', 'G': 'G', 'I': 'I', 'K': 'K', 'M': 'M', 'O': 'O', 'Q': 'Q', 'S': 'S', 'U': 'U', 'W': 'W', 'Y': 'Y', 'a': 'a', 'c': 'c', 'e': 'e', 'g': 'g', 'i': 'i', 'k': 'k', 'm': 'm', 'o': 'o', 'q': 'q', 's': 's', 'u': 'u', 'w': 'w', 'y': 'y'}

Their output:

{'A': 'A', 'C': 'C', 'B': 'B', 'E': 'E', 'D': 'D', 'G': 'G', 'F': 'F', 'I': 'I', 'H': 'H', 'K': 'K', 'J': 'J', 'M': 'M', 'L': 'L', 'O': 'O', 'N': 'N', 'Q': 'Q', 'P': 'P', 'S': 'S', 'R': 'R', 'U': 'U', 'T': 'T', 'W': 'W', 'V': 'V', 'Y': 'Y', 'X': 'X', 'Z': 'Z', 'a': 'a', 'c': 'c', 'b': 'b', 'e': 'e', 'd': 'd', 'g': 'g', 'f': 'f', 'i': 'i', 'h': 'h', 'k': 'k', 'j': 'j', 'm': 'm', 'l': 'l', 'o': 'o', 'n': 'n', 'q': 'q', 'p': 'p', 's': 's', 'r': 'r', 'u': 'u', 't': 't', 'w': 'w', 'v': 'v', 'y': 'y', 'x': 'x', 'z': 'z'}

I've already used python to test and compare and they are indeed dicts comprised of the same keys:values. Any advice would be grand. (This is for an online MIT class. There are normally forums, but they are down while there is an exam available. This is NOT for the exam,

No correct solution

OTHER TIPS

Dictionaries in Python don't preserve a particular order. So you can't be sure that the keys and values will be read in the order that they were (a) originally created or (b) last updated.

Quite obviously, the two dictionaries are the same though.

Edit: dict1 == dict2 is sufficient to prove whether two dictionaries are the same; you don't have to check each key/value piecewise.

Just another note, you should be using the modulus % operator instead of - 26.

Replace

    if sUpperCase.index(c) + shift > 25:

        dCode[c] = sUpperCase[(sUpperCase.index(c) + shift) - 26]

    else:

        dCode[c] = sUpperCase[(sUpperCase.index(c) + shift)]

With

    dCode[c] = sUpperCase[(sUpperCase.index(c) + shift) % 26]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow