[Tutor] Python challenge and decryption

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Dec 23 18:32:50 CET 2011


Posting as HTML caused your indentation to be all wrong. 
Try to post as plain text to remove those errors. 
Thankfully your program is not very complicated.

import string
def decrypt(cypheredText, shiftedCypherNumber):
    '''
This function will take two arguments. The first is the cyphered text, the second
is the number of characters we need to shift the text so we can decrypt it.
This is a Caesar cypher. 
    '''
    for letter in cypheredText:
        asciiValue = ord(letter)
        if asciiValue in range(97, 123):
            asciiValue += shiftedCypherNumber
            if asciiValue > 122: 
                asciiValue -= 26
        newLetter = chr(asciiValue)
        text = list()
        text.append(newLetter)
    joinedText = ' '.join(text)
    return joinedText

text = 'g fmnc wms bgblr rpylqjyrc gr zw fylb'
a = decrypt(text, 2)
print a

You have very little range checking which means that it is 
very likely to not work (or behave oddly) for unexpected values.

The biggest flaw I see is that your list is created at the 
wrong level. It should be outside the for loop. The problem
with list creation being in the for loop is that you create
a new list each iteration of the for loop and what happens 
to the old list? You lose it. If you created the list 
outside the for loop and my indentation above is incorrect
then you created and appended to it after finishing your for loop.
This would be incorrect because what would happen to all the values
calculated inside the for loop? They would be lost. 
Either way, it is incorrect. You should declare text before 
the for loop and then append to it inside the for loop. 

The above is why you only get one letter and also why 
text=list(newLetter) is incorrect for your problem.
 
Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list