Add new exercises and a Caeser Cipher tool v1.0

This commit is contained in:
Josh Mudge 2018-09-22 17:42:18 -06:00
parent bc5c9c0b77
commit fdb7bccb73
8 changed files with 176 additions and 29 deletions

56
caeser-cipher.py Normal file
View File

@ -0,0 +1,56 @@
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
'''

21
ex10-breakallthngs.py Normal file
View File

@ -0,0 +1,21 @@
tabby_cat "\tI'm tabbed in."
persian_cat "I'm split\non a line."
backslash_cat "I'm \\ a \\ cat."
fat_cat =
I'll do a list:
\asdft* Cat food
\t* Fishes
\tasdf* Catnip\n\t* Grass
\asdt* Yo \adfsa Yasfoaa\azbcow\zznasabasbbdrwhat\kvcow
\U0001F60
printtabby_cat)
printpersian_cat)
printbackslash_cat)
printfat_cat)
1. Got them on a study sheet minus hex, others not explained.
2. don't know
3. Done. See fat_cat

21
ex10.py Normal file
View File

@ -0,0 +1,21 @@
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishes
\t* Catnip\n\t* Grass
\t* Yo \a Yo\bcow\rwhat\vcow
\U0001F60D
'''
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
# 1. Got them on a study sheet - hex, others not explained.
# 2. don't know
# 3. Done. See fat_cat

34
ex11_Fun.py Normal file
View File

@ -0,0 +1,34 @@
# I had some fun with this one.
shortInches = False # set shortInches to False
#print("How old are you?", end=' ')
#age = input()
height = input("How tall are you? Please format your height like 5'9 :")
#print("How much do you weigh?", end=' ')
#weight = input()
#print(f"So, you're {age} old, {height} tall and {weight} heavy.")
first = height[0] # Get first letter of the height variable.
second = height[1] # Get second letter of the height variable.
third = height[2] # Get third letter of the height variable.
last = height[-1] # Get last letter of the height variable.
if height[2] == height[-1]: # If the third and last letters of height are the same, set shortInches to true.
shortInches = True
#like = input("Do you like this?")
#print(like)
#1. Done.
#2 & #3 That was a lot of fun.
if shortInches == True: # If shortInches is true, then print this
print("You are " + first + " feet " + third + " inches tall.")
else: # Otherwise, print this
print("You are " + first + " feet " + third + last + " inches tall.")
if "\"" in height: # This is good fun. If the user uses inches instead of feet by accident, let them know sarcastically.
print("Wow! You are " + height[0] + " inches tall. That's SO TALL!")
if not "\"" in height: # Otherwise, it's not that interesting. if not
print("A boring normal height.")

29
ex7.py
View File

@ -1,29 +0,0 @@
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.
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.

14
ex8.py Normal file
View File

@ -0,0 +1,14 @@
formatter = "{} {} {} {} {}" # Create 4 slots to fill.
print(formatter.format(1, 2, 3, 4, 5)) # Fill them with 1, 2, 3, 4
print(formatter.format("one", "two", "three", "four", "five")) # Spell it out.
print(formatter.format(True, False, False, True, False)) # True, false, false, true
print(formatter.format(formatter, formatter, formatter, formatter, formatter)) # Print itself 4 times.
print(formatter.format(
"Try your",
"Own text here",
"Maybe a poem",
"Or a song about fear",
"Or a song of redemption."
)) # One long string, don't forget the commas.

16
ex9-mod.py Normal file
View File

@ -0,0 +1,16 @@
# Here's some new strange stuff, remember type it exactly.
Cows = "Fred Mary Freddy Faith Freddy Jr. Felicity"
Dogs = "\n\nCharlie\nChocolate\nGeorge\nCharlie Jr.\nTerri\nFifi\nGoldie\nTimmy\nTabby"
print("Here are the dogs: ", Dogs)
print("\nHere are the cows: ", Cows)
print("""
But who likes Stubbly Grass Nibbles?
Why, Toby of course.
And Blueby is obsessed. The shopping is strong with this one.
Toby Jr. would rather invent a Stubbly Grass Generator.
Bubbles loves clothes, but refuses to go on the ridiculous shopping sprees \n of her mother.
""")

14
ex9.py Normal file
View File

@ -0,0 +1,14 @@
# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days: ", days)
print("Here are the months: ", months)
print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")