57 lines
3.0 KiB
Python
57 lines
3.0 KiB
Python
import re # Import Regex tools
|
|
import string # Import string tools
|
|
ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user.
|
|
ciphertext = re.sub (r'([^a-zA-Z ]+?)', '', ciphertext) # Remove all non-letters.
|
|
ciphertext = ciphertext.lower() # Make it all lowercase.
|
|
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.
|
|
shiftNum = input("Please enter the shift used:") # Get input
|
|
|
|
for c in ciphertext: # For every letter in the ciphertext
|
|
#Upletters = list(string.ascii_uppercase)
|
|
while letterPos < 26: # While the letter's position in the alphabet is less than
|
|
if ciphertext[letterCount] == letters[letterPos]: # Match the letter in the ciphertext to a letter in the alphabet and once they match, continue.
|
|
letter = int(letterPos) + int(shiftNum) # Take the position of the letter and the shift number. NOTE: catch spaces.
|
|
if letter > 25:
|
|
letter = letter - 26
|
|
if letter < 0:
|
|
letter = letter + 26
|
|
answer = answer + letters[letter]
|
|
|
|
letterPos = letterPos + 1
|
|
if letterPos > 25:
|
|
letterPos = letterPos - 26
|
|
|
|
letterCount = letterCount + 1
|
|
|
|
print("\nYour enciphered text is: " + answer)
|
|
|
|
#alphabet = '"abcd","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"' #old alphabet string for an array in JS
|
|
#print(re.sub (r'([^a-zA-Z]+?)', '', alphabet)) # strip everything except letters
|
|
|
|
'''
|
|
On line #4
|
|
This feeds a regex to re.sub literally "If it doesn't match letters or spaces, convert it to nothing, process this variable."
|
|
Scrub extranous info or typos.
|
|
r marks it as a string literal (no escaping needed)
|
|
[] indicates a set of characters, special characters become normal.
|
|
^ = start of the string. In a set like this, it matches anything that is not in the string.
|
|
a-z matches lowercase letters.
|
|
Z-Z matches caps letters
|
|
matches a space
|
|
+? essential means "try to match this till you can't"
|
|
More info: https://docs.python.org/3/library/re.html
|
|
'''
|
|
|
|
'''
|
|
### Stackoverflow thanks to:
|
|
First char of string: https://stackoverflow.com/questions/48973202/how-to-get-first-char-of-string-in-python
|
|
Length of string: https://stackoverflow.com/questions/4967580/how-to-get-the-size-of-a-string-in-python
|
|
Regex to scrub var of extranous info/symbols: https://stackoverflow.com/questions/44315941/regex-to-strip-all-numbers-and-special-characters-but-space-and-letters
|
|
Proper syntax for if blocks: https://stackoverflow.com/questions/37376516/python-check-if-multiple-variables-have-the-same-value
|
|
https://www.tutorialspoint.com/python/python_if_else.htm
|
|
Find substring in a string: https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method
|
|
'''
|