1

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,

Community
  • 1
  • 1
  • in python 2.7+ you can use `OrderedDict` from `collections`, however using dictionaries for this is highly inefficient. – Kimvais Nov 04 '12 at 07:09
  • Efficient or not, I'm supposed to return the cipher as a dict. – Seth Bourque Nov 04 '12 at 07:15
  • @SethBourque: I don't want to appear rude or so, but aren't you also supposed to [search the forums](http://stackoverflow.com/search?q=caesar) before posting a question? – georg Nov 04 '12 at 10:44
  • That is somewhat pointless. You're pointing me to a question regarding changing strings with the cipher when my cipher works and I'm having a problem with returning incorrect output because of the varying order of two dicts – Seth Bourque Nov 04 '12 at 11:37
  • Just because you have to _return_ a dictionary, does not mean that you have to work with dictionaries all the way – Kimvais Nov 04 '12 at 11:43

2 Answers2

3

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.

Andy Casey
  • 307
  • 1
  • 2
  • 11
1

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]
Tim
  • 11,710
  • 4
  • 42
  • 43