''' functions.py A collection of miscellaneous functions, for the CS 117 assignment due Tuesday, Feb 13, 2007. ''' def isPrime(n): ''' n -- an integer returns -- True if n is a prime number, and False otherwise. ''' pass def isPalindrome(s): ''' s -- a string returns -- True if the specified string is a palindrome, and False otherwise. A palindrome is a string that reads the same forwards and backwards. For example, 'rats live on no evil star' and 'Sit on a potato pan, Otis!' are both palindromes. For this assignment, you may choose the easy route or the harder route. The easy version will identify 'rats live on no evil star' as a palindrome, but will reject 'Sit on a potato pan, Otis!' because of the punctuation, the different spacing, and different capitalization. The harder version will ignore spacing, punctuation, and capitalization, thus accepting 'Sit on a potato pan, Otis!', 'Satan, oscillate my metallic sonatas.', and others. The grader will give a bonus point or two for a good job on the harder version. ''' pass def encrypt(plaintext, shiftDistance): ''' plaintext -- a string (intended to be a message to be encrypted) shiftDistance -- an int used in the encryption algorithm (see below) returns -- a string containing the encrypted form of the plaintext This function should encrypt the plaintext using the 'Caesar Cipher'. That is, every letter in the string will be shifted by shiftDistance letters through the alphabet. For example, if plaintext is 'my zebra' and shiftDistance is 4, this function should return 'qc difve'. If shiftDistance is -4, then 'my zebra' would yield 'iu vaxnw'. Note that if the shiftDistance would move a letter past the end of the alphabet, you should wrap around to the beginning of the alphabet. That's why the 'y' + 4 = 'c' in the example above. Notes: -- The % operator can help you here. For example, (-4 % 26) is 22. -- To add a number to a letter, you have to play a trick like so: oldLetter = 'a' newLetter = chr(ord(oldLetter) + 3) ord takes a character and returns its Unicode value (in this case 97). chr takes a Unicode value (in this case 100) and turns it back into a character (in this case 'd'). ''' pass def decrypt(ciphertext, shiftDistance): ''' ciphertext -- a string, assumed to have been previously encrypted using 'encrypt' shiftDistance -- an int used in the decryption algorithm returns -- a string containing the plaintext corresponding to ciphertext This function decrypts the specified ciphertext using the Caesar Cipher. For example, if ciphertext is 'qc difve' and shiftDistance is 4, this function will return 'my zebra'. If you do a good job on decrypt, you may find decrypt very easy to write. ''' pass # Some minimal testing code. Modfiy as you see fit. if __name__ == '__main__': n = input('Number, please: ') if isPrime(n): print n, 'is prime' else: print n, 'is not prime' s = raw_input('Sentence, please: ') if isPalindrome(s): print '"%s" is a palindrome' % s else: print '"%s" is not a palindrome' % s message = raw_input('Message: ') n = input('Shift distance: ') encryptedMessage = encrypt(message, n) decryptedMessage = decrypt(encryptedMessage, n) print 'Encrypted message:', encryptedMessage print 'Decrypted message:', decryptedMessage