Skip to content

Instantly share code, notes, and snippets.

@nazt
Created April 6, 2010 07:24
Show Gist options
  • Save nazt/357317 to your computer and use it in GitHub Desktop.
Save nazt/357317 to your computer and use it in GitHub Desktop.
########## BEGIN DESCRIPTION ############################################
##
## Caeser Cipher Decryption.
## Write a function named decrytpion which accepts an encrypted English
## phrase - ciphertext - (parameter 1) and a shift value (parameter 2)
## and decrypts the phrase into plaintext.
##
## *See tests for examples of input and expected output*
##
########## END DESCRIPTION ##############################################
########## BEGIN PAIR DETAILS #####################################
##
## Student Name: *** WRITE YOUR NAME HERE***
##
## Student Number: *** WRITE YOUR STUDENT NUMBER HERE ***
##
## Student Name: *** WRITE YOUR NAME HERE***
##
## Student Number: *** WRITE YOUR STUDENT NUMBER HERE ***
##
########## END PAIR DETAILS #######################################
########## BEGIN MARKER'S FEEDBACK ###################################
##
##
########## END MARKER'S FEEDBACK #####################################
########## BEGIN ACCEPTANCE TESTS ####################################
##
## This section contains the minimum tests that your program must
## pass. Please note, 'hard-coded' solutions are not acceptable.
##
'''
## Tests for Decryption
###### Test 1 - simple phrase, standard shift
>>> decrypt('wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj', 3)
'the quick brown fox jumps over the lazy dog'
###### Test 2 - simple phrase - no shift
>>> decrypt('the quick brown fox jumps over the lazy dog', 0)
'the quick brown fox jumps over the lazy dog'
###### Test 3 - simple phrase - full 26 character shift
>>> decrypt('the quick brown fox jumps over the lazy dog', 26)
'the quick brown fox jumps over the lazy dog'
###### Test 4 - simple phrase - two full shifts
>>> decrypt('the quick brown fox jumps over the lazy dog', 52)
'the quick brown fox jumps over the lazy dog'
###### Test 5 - empty string, standard shift
>>> decrypt('', 3)
''
###### Test 6 - one space, standard shift
>>> decrypt(' ', 3)
' '
###### Test 7 - alphabet, shifted once
>>> decrypt('bcdefghijklmnopqrstuvwxyza', 1)
'abcdefghijklmnopqrstuvwxyz'
###### Test 8 - alphabet, shifted 25
>>> decrypt('zabcdefghijklmnopqrstuvwxy', 25)
'abcdefghijklmnopqrstuvwxyz'
###### Test 9 - alphabet with spaces, standard shift
>>> decrypt('d e f g h i j k l m n o p q r s t u v w x y z a b c', 3)
'a b c d e f g h i j k l m n o p q r s t u v w x y z'
###### Test 10 - simple phrase, shifted 13
>>> decrypt('gur enva va fcnva fgnlf znvayl va gur cynva', 13)
'the rain in spain stays mainly in the plain'
'''
##
########## END ACCEPTANCE TESTS ######################################
########## BEGIN Decryption Code ###########################
##
##
## Algorithm:
##
## *** PUT YOUR ALGORITHM HERE ***
## Program code:
##
## *** PUT YOUR PROGRAM CODE BELOW ***
# Decrypt the given cyphertext using the Caesar cipher. Shift by the given
# shift value.
def decrypt(ciphertext,shift):
out_str = ''
for c in ciphertext:
if(ord(c)==32):
out_str+=chr(32)
else:
out_str+=chr((((ord(c)-ord('a'))-shift)%26)+ord('a'))
return out_str
##
########## END Decryption Code #############################
########## BEGIN AUTOMATIC TESTING ###################################
##
## The following code will automatically run the acceptance tests
## when the program is "run". Do not change anything in this
## section. If you want to prevent tests from running, comment
## out the code below, but ensure that the code is uncommented when
## you submit your program
##
# Run the tests if this is the main program:
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)
##
########## END AUTOMATIC TESTING #####################################
@nazt
Copy link
Author

nazt commented Apr 6, 2010

ok
1 items had no tests:
main.decrypt
1 items passed all tests:
10 tests in main
10 tests in 2 items.
10 passed and 0 failed.
Test passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment