""" CS150 Fall Lab 3 Name: Section: Creativity: """ from random import randint, seed ALPHABET = "abcdefghijklmnopqrstuvwxyz " #--------------------------------------------------------- # Fools functions # TODO: Write your fools function(s) here #--------------------------------------------------------- # Caesar's method def shift_letter(letter, num): """ Shifts a letter up num places in the alphabet with wraparound. Args: letter: A single character num: Integer distance to shift letter Returns: Character in ALPHABET num distance from letter """ # Get the index of the current letter index = ALPHABET.find(letter) # We use the mod operator (%) for wraparound return ALPHABET[(index + num) % len(ALPHABET)] # TODO: Write your caesar_encrypt function here #--------------------------------------------------------- # Substitution cipher def splice(message, letter): """ Splices out letter from message and returns the remaining message. letter can only occur ONCE in the message For example: >>> splice("abcdefg", "f") 'abcdeg' Args: message: String letter: A string to be removed from message Returns: String message with letter removed. """ # TODO: fill in the details of this function def keygen(password): """ Given a string password, generates a new random key. A key consists of a random ordering of the letters in ALPHABET. Args: password: String password used to generate key Return: Key string """ remaining = ALPHABET key = "" seed(password) for dummy in range(len(ALPHABET)): index = randint(0, len(remaining)-1) next_letter = remaining[index] key = key + next_letter remaining = splice(remaining, next_letter) return key # TODO: Fill in your substitution functions here