Add frequency function.

This commit is contained in:
Josh Mudge 2018-09-22 20:40:39 -06:00
vanhempi fdb7bccb73
commit b0d9c1542c
2 muutettua tiedostoa jossa 47 lisäystä ja 13 poistoa

Näytä tiedosto

@ -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)

29
ex7-dots.py Normal file
Näytä tiedosto

@ -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.