From b0d9c1542c3b243233fedc9323dda75fdc4775b5 Mon Sep 17 00:00:00 2001 From: Josh Mudge Date: Sat, 22 Sep 2018 20:40:39 -0600 Subject: [PATCH] Add frequency function. --- caeser-cipher.py | 31 ++++++++++++++++++------------- ex7-dots.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 ex7-dots.py diff --git a/caeser-cipher.py b/caeser-cipher.py index 1f69055..eee6f1b 100644 --- a/caeser-cipher.py +++ b/caeser-cipher.py @@ -1,38 +1,43 @@ import re # Import Regex tools import string # Import string tools +import collections + ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user. +shiftNum = input("Please enter the shift used:") # Get input ciphertext = re.sub (r'([^a-zA-Z ]+?)', '', ciphertext) # Remove all non-letters. -ciphertext = ciphertext.lower() # Make it all lowercase. +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 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 + while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue. 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 = int(letterPos) + int(shiftNum) # Take the position of the letter and the shift number and add them for the enciphered letter. + if letter > 25: # If the enciphered letter's position is not valid because it is too high, fix it by rotating around the alphabet. letter = letter - 26 if letter < 0: - letter = letter + 26 - answer = answer + letters[letter] + letter = letter + 26 # If the enciphered letter's position is not valid because it is too low, fix it by rotating around the alphabet. + answer = answer + letters[letter] # Add letters together to get the enciphered text. - letterPos = letterPos + 1 - if letterPos > 25: + letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.) + + if letterPos > 25: # If the letter position 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 +def freq(): + print(collections.Counter(answer).most_common()[0][0]) # Find most common letter https://stackoverflow.com/questions/47251934/how-to-count-the-most-frequent-letter-in-a-string + +print("\nYour enciphered text is: " + answer) # NOTE TO SELF: catch spaces for nice formatting in answer. +freq() ''' -On line #4 +On line #4 Regex 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) diff --git a/ex7-dots.py b/ex7-dots.py new file mode 100644 index 0000000..6afb845 --- /dev/null +++ b/ex7-dots.py @@ -0,0 +1,29 @@ +print("Mary had a little lamb.") # Print string +print("Its fleece was white as {}.".format('snow')) # Print string with format. +print("And everywhere that Mary went.") # Print string. + +with open('out.txt', 'w') as f: + print("." * 1000000000, file=f) # A little fun with a billion dots being saved to a file. + +end1 = "C" # Set variable to a string. +end2 = "h" # Set variable to a string. +end3 = "e" # Set variable to a string. +end4 = "e" # Set variable to a string. +end5 = "s" # Set variable to a string. +end6 = "e" # Set variable to a string. +end7 = "B" # Set variable to a string. +end8 = "u" # Set variable to a string. +end9 = "r" # Set variable to a string. +end10 = "g" # Set variable to a string. +end11 = "e" # Set variable to a string. +end12 = "r" # Set variable to a string. + + +# watch end = ' ' at the end. try removing it to see what happens. +print(end1 + end2 + end3 + end4 + end5 + end6, end=' ') # Print combined variables without a space in between on the same line as the next set of them with a space in between: +print(end7 + end8 + end9 + end10 + end11 + end12) # Print variables without spaces in between + +#1. Done +#2. Done + +# Break it.