Find Method Clone

# find_clone.py
def find(letter, string):
    for index in range(len(string)):	# look for the letter in the string
        if string[index] == letter:		# if letter found in string, return the index
            return index							
    return -1			# if letter not found in string, return -1

print(find("t", "Petra"))   # should return 2
print(find("z", "Petra"))   # should return -1

Count Method Clone

# count_clone.py
def count(letter, string):
    for character in letter:
        
print(count("c", "soccer"))     # returns 2
print(count("r", "soccer"))     # returns 1
print(count("x", "soccer"))     # returns 0

Remove Duplicates Problem

Write a function called remove_dups that takes a string and creates a new string by only adding those characters that are not already present. In other words, there will never be a duplicate letter added to the new string.

# remove_dups.py
def remove_dups(string):
  
print(remove_dups("banana sandwich")

Substitution Cipher Problem

Write a function that implements a substitution cipher. In a substitution cipher one letter is substituted for another to garble the message. For example A -> Q, B -> T, C -> G etc. your function should take two parameters, the message you want to encrypt, and a string that represents the mapping of the 26 letters in the alphabet. Your function should return a string that is the encrypted version of the message.

# sub_cipher.py

import string

def encrypt(message):
    # set encryption key
    alphabet = string.ascii_lowercase			# all lowercase letters
    key = "qwertyuiopasdfghjklzxcvbnm"
    punctuation = string.punctuation + " "	# all punctuation (need to add a space)

    # encrypt message
    encryption = ""
    for character in message.lower():
        if character in punctuation:						# keep punctuation the same
            encryption = encryption + character
        else:
            index = alphabet.find(character)		 # find the letter's index in alphabet
            encryption = encryption + key[index] # replace with same index letter in key

    return encryption

def decrypt(message):
    # set decryption key
    alphabet = string.ascii_lowercase
    key = "qwertyuiopasdfghjklzxcvbnm"
    punctuation = string.punctuation + " "

    # decrypt message
    decryption = ""
    for character in message.lower():
        if character in punctuation:
            decryption = decryption + character
        else:
            index = key.find(character)								# find letter's index in key
            decryption = decryption + alphabet[index] # replace with same index in alphabet

    return decryption.capitalize()

message = "Storm the walls at dawn!"

# encrypt message and return to user
encrypted = (encrypt(message))
print(encrypted)

# decrypt message and return to user
decrypted = decrypt(encrypted)
print(decrypted)