-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial3_Sequences_HosnaHamdieh.py
More file actions
268 lines (248 loc) · 9.95 KB
/
Tutorial3_Sequences_HosnaHamdieh.py
File metadata and controls
268 lines (248 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 26 22:43:13 2021
@author: Hosna Hamdieh
"""
##################################### Tutorial 3: Sequences #####################################################
# This program includes 6 functions around sequences and it is automatically run asking the user to choose one of them by entering a number
# IT will continue while the user is entering a number and when they just press Enter it will stop
######################################## Q1: Birth Trees ########################################
def Birth_trees():
print("""Do you want to know what your birth tree is?
You are in the right place
""")
Month=input("""What is your month of birth?
Month of birth: """)
BT={"January": "Apple",
"February": "Willow",
"March": "Maple",
"April": "Elm",
"May": "Fig",
"June": "Cherry",
"July": "Walnut",
"August": "Oak",
"September": "Palm",
"October": "Olive",
"November": "Citrus",
"December": "Spruce"}
try:
UBT = BT[Month]
print("Your birth tree is {} since you have entered {} as your month of birth".format(UBT,Month))
print()
except:
print("Your answer is not valid try again")
#Birth_trees()
####################################### Q2: Hello World ###########################################
def Hello_World():
print("Lets say hi to the world together")
textFile=open("countries.txt","r")
textRead=textFile.read()
HWord=open("helloword.txt","a")
text=textRead.split("\n")
HW=[]
for i in text:
hc="Hello "+i
HW.append(hc)
HelloWord="""
""".join(HW)
print("""Hello word:
""", HelloWord)
print(HelloWord,file=HWord)
textFile.close()
HWord.close()
#Hello_World()
##################################### Q3: Vocabulary #############################################
def Vocabulary():
Answer = int(input("""Enter 1 or 2 for to calculate the average word lenght
1. The average lenght of english words
2. The average word lenght in one text file
Answer: """))
if Answer == 1:
FileName = "English_Words.txt"
elif Answer == 2:
FileName = input("""What is the name of your text file? (FileName.txt)
FileName: """)
else:
print("Error: Your input is not valid, try again")
WL = []
WDL = {}
textFile = open(FileName,"r")
text = textFile.read()
SWL = 0
words = text.split()
for word in words:
wl = len(word)
WL.append(wl)
SWL += wl
WDL[word] = wl
AWL = SWL/len(words)
print(f"The average word length is {AWL}")
textFile.close()
#Vocabulary()
################################### Q4: Password Generator #####################################
def Password():
Answer = input('''Do you like to have only one password generated or more? (type 1 or more)
Answer: ''')
if Answer == "1":
Password_Generator()
else:
Password_save()
def Password_Generator():
import string
special_letters = string.punctuation
#print(special_letters)
letters = string.ascii_letters
#print(letters)
numbers = string.digits
#print(numbers)
from random import choice,randint
characters = letters + special_letters + numbers
print("Your password will include different characters randomly picked")
password = "".join(choice(characters) for x in range(randint(10, 16)))
print ("Your password is: ",password)
return password
#Password_Generator()
def Password_save():
PassFile = open("passwords.txt","a")
try:
n = int(input("""How many passwords do you need?
n= """))
Password_list = []
for i in range(n):
Pass = Password_Generator()
Password_list.append(Pass)
PassText=str(Password_list)
print(PassText,file=PassFile)
PassFile.close()
except:
print("Your answer is not valid try again")
PassFile.close()
#Password_save()
################################## Q5: Caesar Cipher #########################
# By running the decipher code testing all keys we can find the answer for part c of the question (Deciphering SIOBUPYWLUWEYXNBTWIXY):
# Decoded massage using 20 as the key is: YOUHAVECRACKEDTHECODE
# or in another words YOU HAVE CRACKED THE CODE
# So even when we do not have the key we can simply test it with all keys and check to see which one makes sense
def Caesar_cipher(letter,n):
pos = ord(letter)-65 #position for mod math
e = (pos + n)%26
#convert back to unicode value and get new character
new_chr = chr(e+65)
return new_chr
def Coding():
Textin = input("Enter a message: ").upper()
n = int(input("Enter a value for n: "))
M = Textin.split()
NM = []
for word in M:
NW = ""
for ch in word:
NW += Caesar_cipher(ch,n)
NM.append(NW)
new_massage = "".join(NM)
print("The massage is coded into: ", new_massage)
print()
def Decriptor_Ceasar_cipher(letter,n):
pos = ord(letter)-65
e = (pos - n)%26
new_chr = chr(e+65)
return new_chr
def DeCoding():
Textin = input("Enter a message: ").upper()
A=input("""Do you have the key to decode the text? (yes/no)
Answer= """).lower()
if A == "yes":
n = int(input("Enter a value for n: "))
NM = Textin.split()
NME=[]
for word in NM:
NWE = ""
for ch in word:
NWE += Decriptor_Ceasar_cipher(ch,n)
NME.append(NWE)
new_massageE =" ".join(NME)
print("Lets see if we can change it back: ",new_massageE)
elif A == "no":
print("""Not having the key makes things a bit harder
We can give you all possible choices using different keys
It is up to you to find the one that makes sense""")
for n in range(1,26):
NM = Textin.split()
NME=[]
for word in NM:
NWE = ""
for ch in word:
NWE += Decriptor_Ceasar_cipher(ch,n)
NME.append(NWE)
new_massageE =" ".join(NME)
print("Decoded massage using {} as the key is: ".format(n),new_massageE)
def Coding_Decoding():
Answer=input("""Do you want to code or decode a massage: (Type c for code and d for decode)
Answer: """).lower()
if Answer == "c":
Coding()
elif Answer == "d":
DeCoding()
else:
print("Error: Your answer is not valid try again")
#Coding_Decoding()
################################################# Q6: Latin Keyboard ##########################################
from collections import Counter
import re
def Latin_Keyboard():
infile = open("CeasarBook.txt","r")
String = infile.read().lower()
Words = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%\@\#\;\!\$\,\:)*\b', '', String, flags=re.MULTILINE)
WordsC = re.sub("[^a-zA-Z\s]", " ", Words)
# print(WordsC)
listOfWords = WordsC.split()
# print(listOfWords)
LastLetter = [word[-1] for word in listOfWords]
Common_LastLetters = Counter(LastLetter).most_common(80)
print("""Common last letters in Latin:
""",Common_LastLetters)
text = String.replace(" ", "")
# print(text)
text = text.replace("\n","")
Text = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%\@\#\;\!\$\,\:)*\b', '', text, flags=re.MULTILINE)
TextC = re.sub("[^a-zA-Z\s]", " ", Text)
AllLetters = [letter for letter in TextC]
# alphabet = ["a","b","c",]
Common_letters = Counter(AllLetters).most_common(80)
print("""Common letters in Latin:
""",Common_letters)
Ceasar_keyboard = f""" I II III IV V VI VII VIII IX X XX - = <-
Tab {Common_letters[19][0]} {Common_letters[17][0]} {Common_letters[11][0]} {Common_letters[9][0]} {Common_letters[8][0]} {Common_letters[10][0]} {Common_letters[16][0]} {Common_letters[18][0]} {Common_letters[20][0]} {Common_letters[21][0]} [ ] slash
CL {Common_letters[15][0]} {Common_letters[13][0]} {Common_letters[7][0]} {Common_letters[6][0]} {Common_letters[5][0]} {Common_letters[3][0]} {Common_letters[4][0]} {Common_letters[1][0]} {Common_LastLetters[3][0]} {Common_LastLetters[2][0]} ; ' Enter
Shift {Common_letters[-1][0]} {Common_letters[-2][0]} {Common_letters[3][0]} {Common_letters[3][0]} {Common_letters[12][0]} {Common_LastLetters[1][0]} {Common_LastLetters[0][0]} , . / Shift
Ctrl fn win Alt space Alt Car Ctr """
print("""Ceasar Keyboard is designed based on having the most used letters in the middle row
having the most used last letters near the punctuation marks
and less used letters far from reach:
""",Ceasar_keyboard)
infile.close()
#Latin_Keyboard()
##################################### Tutorial 3: Sequences #####################################################
def Sequences():
ListOfFunctions = [Birth_trees, Hello_World, Vocabulary, Password, Coding_Decoding, Latin_Keyboard]
try:
Answer = int(input("""Welcome to Sequences
We have 6 functions that you could test listed below. Chose the number related to the function so that you could test it
Have fun
Note: Please type one number at a time and press Enter if you want to quit
List of functions:
1= Birth_trees, 2= Hello_World, 3= Vocabulary, 4= Password, 5= Coding_Decoding, 6= Latin_Keyboard
Answer: """))
while Answer != "":
try:
i = int(Answer) - 1
Function = ListOfFunctions[i]
Function()
Answer = input("""Do you want to try another? type another number from 1 to 6 or Enter if you want to quit
Answer: """)
except:
print("Your answer is not valid try again")
except:
print("Your answer is not valid")
Sequences()
################################################ Finished #############################################