Update using passed parameters instead of global variables.
This commit is contained in:
parent
baa81c46d1
commit
024c033ac2
|
@ -1,4 +1,4 @@
|
||||||
# Python Caeser Cipher Helper v1.0.2
|
# Python Caeser Cipher Helper v1.1.0
|
||||||
# Usage (encrypt): python3 caeser-cipher.py encrypt secretmessage
|
# Usage (encrypt): python3 caeser-cipher.py encrypt secretmessage
|
||||||
# Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage
|
# Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage
|
||||||
|
|
||||||
|
@ -19,19 +19,14 @@ shiftNum = input("Please enter the shift you would like to use (Just hit enter i
|
||||||
ciphertext = re.sub (r'([^a-zA-Z]+?)', '', ciphertext) # Remove all non-letters.
|
ciphertext = re.sub (r'([^a-zA-Z]+?)', '', ciphertext) # Remove all non-letters.
|
||||||
ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/
|
ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/
|
||||||
letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet
|
letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet
|
||||||
letterCount = 0 # Set variable for keeping track of which letter in the ciphertext we're on.
|
|
||||||
letterPos = 0 # Set variable for keeping track of the letter's position in the alphabet.
|
|
||||||
answer = "" # The solution.
|
|
||||||
commonletter = 0
|
|
||||||
|
|
||||||
def encrypt():
|
def encrypt(ciphertext, shiftNum, letters): # Grab parameters
|
||||||
# Grab global variables.
|
|
||||||
global ciphertext
|
# Create local variables for this function.
|
||||||
global shiftNum
|
|
||||||
global letters
|
letterCount = 0
|
||||||
global letterCount
|
letterPos = 0
|
||||||
global letterPos
|
answer = ""
|
||||||
global answer
|
|
||||||
|
|
||||||
for c in ciphertext: # For every letter in the ciphertext
|
for c in ciphertext: # For every letter in the ciphertext
|
||||||
while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue.
|
while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue.
|
||||||
|
@ -56,16 +51,13 @@ def encrypt():
|
||||||
print("\nYour enciphered text is: " + answer)
|
print("\nYour enciphered text is: " + answer)
|
||||||
|
|
||||||
|
|
||||||
def decrypt():
|
def decrypt(ciphertext, shiftNum, letters, commonletter): # Grab parameters
|
||||||
|
|
||||||
# Grab global variables.
|
# Create local variables for this function.
|
||||||
global ciphertext
|
|
||||||
global shiftNum
|
letterCount = 0
|
||||||
global letters
|
letterPos = 0
|
||||||
global letterCount
|
answer = ""
|
||||||
global letterPos
|
|
||||||
global answer
|
|
||||||
global commonletter
|
|
||||||
|
|
||||||
for c in ciphertext: # For every letter in the ciphertext
|
for c in ciphertext: # For every letter in the ciphertext
|
||||||
while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue.
|
while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue.
|
||||||
|
@ -90,13 +82,13 @@ def decrypt():
|
||||||
print("\nYour decrypted text is: " + answer)
|
print("\nYour decrypted text is: " + answer)
|
||||||
|
|
||||||
|
|
||||||
def freq():
|
def freq(ciphertext):
|
||||||
# Grab global variables.
|
# Create local variables for this function.
|
||||||
global ciphertext
|
|
||||||
global commonletter
|
commonletter = 0
|
||||||
global letterPos
|
letterCount = 0
|
||||||
global shiftNum
|
letterPos = 0
|
||||||
print(shiftNum)
|
answer = ""
|
||||||
|
|
||||||
commonletter = collections.Counter(ciphertext).most_common()[0][0] # Find most common letter and thus the shift https://stackoverflow.com/questions/47251934/how-to-count-the-most-frequent-letter-in-a-string
|
commonletter = collections.Counter(ciphertext).most_common()[0][0] # Find most common letter and thus the shift https://stackoverflow.com/questions/47251934/how-to-count-the-most-frequent-letter-in-a-string
|
||||||
|
|
||||||
|
@ -113,13 +105,13 @@ def freq():
|
||||||
|
|
||||||
letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.)
|
letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.)
|
||||||
|
|
||||||
decrypt() # Decrypt using the frequency found in here.
|
decrypt(ciphertext, shiftNum, letters, commonletter) # Decrypt using the frequency found in here.
|
||||||
|
|
||||||
|
|
||||||
if action == "encrypt":
|
if action == "encrypt":
|
||||||
encrypt()
|
encrypt(ciphertext, shiftNum, letters) # Start the encrypt() function while passing 3 variables as parameters.
|
||||||
if action == "decrypt":
|
if action == "decrypt":
|
||||||
if shiftNum == "":
|
if shiftNum == "":
|
||||||
freq()
|
freq(ciphertext)
|
||||||
else:
|
else:
|
||||||
decrypt()
|
decrypt(ciphertext, shiftNum, letters, commonletter)
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
def no_side_effects(cities, cows):
|
||||||
|
print(cities)
|
||||||
|
cities = cities + ["Birmingham", "Bradford"]
|
||||||
|
print(cities)
|
||||||
|
print(cows)
|
||||||
|
|
||||||
|
locations = ["London", "Leeds", "Glasgow", "Sheffield"]
|
||||||
|
|
||||||
|
no_side_effects(locations, "Fred")
|
Loading…
Reference in New Issue