-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunkce_obsahu_01.py
More file actions
339 lines (291 loc) · 12.1 KB
/
funkce_obsahu_01.py
File metadata and controls
339 lines (291 loc) · 12.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import json
import random
def tharkun_bot(inpt):
#quotes
f = open("data.json")
data = json.load(f)
if inpt == "help":
return("""
*****help****
Hi, and welcome to the my bot. Here follows list of possible inputs.
-quote
-quote:'surname_of_author'
-fact
-rhyme:'language (cze, eng, lat, elf)':'word_to_rhyme'
-cipher:
NOTE: all of them are using small letters of english alphabet - 26 characters
-to_morse:"word_to_translate"
-to_morse:"word_to_translate":"symbol_for '.'":symbol_for '_':symbol_for '/'
-from_morse:"text_to_translate" using '.' '_' and '/', without spaces
-backwards:"text_to_translate"
-skipping:"text_to_translate" ressult is every second character. Between are random letters.
-first_last:"text_to_translate"
-random_rearrange:"text_to_traslate"
-skipp02:"text_to_traslate" every second letter and then back
-caesar:"text_to_traslate":"number_of_move"
-addition:"text_to_traslate":"password"
-backwards_alphabet:"text_to_traslate"
-own_alphabet:"text_to_traslate":'27 characters of new alphabet
-to_numbers:"text_to_traslate"
-from_numbers:"numbers with spaces to translate to letters"
-pair:"text_to_traslate"
-nokia:"text_to_traslate"
-nokia02:text_to_traslate""")
elif inpt == "quote": #returnt random quote of any author
#load of all quotes for random quote. It can be definitely done with initializing of class, but now it is not class, so we let it this way
all_quotes = []
for aut in data["quotes"].keys():
for i in data["quotes"][aut]:
all_quotes.append(i)
return random.choice(all_quotes)
elif "quote" in inpt: #in form "quote:" 'surname_of_author' returns random quote of that specific author
aut = inpt.split(":")[1]
if aut in data["quotes"].keys():
return random.choice(data["quotes"][aut])
else:
return "err - wrong name of author"
elif inpt == "fact": #returns random fact
return random.choice(data["facts"])
elif inpt.startswith("rhyme"): #form "rhyme:"your_language":"your_word, returns list of minimum 10 words with same ending (as many letters as possible)
if len(inpt.split(":")) == 3:
language = inpt.split(":")[1]
word = inpt.split(":")[2]
arr1 = data["rhyme"][language] #"smaller" list. Just words that fits. In the begining bigger of the two, because lists are switsched on beggining of algorithm
arr0 = [] #list of all words from specific languages
nop = 0 #actual number of parity, on begining lenght of word
while len(arr1) >= 10 and nop < len(word):
#print(nop)
arr0 = arr1.copy()
arr1 = []
nop += 1
compare = word[len(word)-nop:]
for i in arr0:
if i.endswith(compare):
arr1.append(i)
if len(arr1) > 10:
return(arr1)
else:
return(arr0)
elif inpt.startswith("cipher"):
inpt_l = inpt.split(":") #list from input split by ":"
if len(inpt_l) >= 3:
result = ""
type_of_cipher = inpt_l[1]
word = inpt_l[2]
if type_of_cipher == "to_morse":
if len(inpt_l) == 3:
for i in word:
result += to_morse(".", "_", "|", i)
else:
if len(inpt_l[3].split(",")) > 1 and len(inpt_l[3]) > 1:
for i in word:
result += to_morse_rnd(inpt_l[3], inpt_l[4], inpt_l[5], i)
#result += to_morse(random.choice(inpt_l[3].split(",")), random.choice(inpt_l[4].split(",")), random.choice(inpt_l[5].split(",")), i)
else:
for i in word:
result += to_morse(inpt_l[3], inpt_l[4], inpt_l[5], i)
elif type_of_cipher == "from_morse":
to_translate = inpt_l[2].split("/")
for i in to_translate:
result += from_morse(i)
elif type_of_cipher == "backwards":
return word[::-1]
elif type_of_cipher == "skipping":
for i in word:
result += i
result += chr(random.randint(97, 122))
elif type_of_cipher == "first_last":
help_res = ""
for i in range(len(word)):
if i % 2 == 0:
result += word[i]
else:
help_res += word[i]
result = result + help_res[::-1]
elif type_of_cipher == "random_rearrange":
word = list(word)
lenght = len(word)
for i in range(lenght):
add = random.choice(word)
result += add
word.remove(add)
elif type_of_cipher == "skipp02":
help_res0 = word[0:len(word)//2+1]
help_res1 = word[len(help_res0):]
help_res1 = help_res1[::-1]
for i in range(len(help_res0)):
result += help_res0[i]
if i < len(help_res1):
result += help_res1[i]
elif type_of_cipher == "caesar":
num = int(inpt_l[3]) #number of positions to move
for i in word:
result += caesar(i, num)
elif type_of_cipher == "big_caesar":
for num in range(0, 26):
for i in word:
result += caesar(i, num)
result += "\n"
elif type_of_cipher == "addition": #caesar based on password
password = inpt_l[3]
while len(word) > len(password):
if len(word) - 2*len(password) >= 0:
password = password + password
else:
password = password + password[0:len(word)-len(password)]
for i in range(len(word)):
result += caesar(word[i], ord(password[i])-96)
elif type_of_cipher == "backwards_alphabet":
for i in word:
result += chr((27-(ord(i)-96))+96)
elif type_of_cipher == "own_alphabet":
alphabet = list(inpt_l[3])
for i in word:
result += alphabet[ord(i)-96]
elif type_of_cipher == "to_numbers": #order in alphabet
for i in word:
result += str(ord(i)-96)+" "
elif type_of_cipher == "from_numbers":
word = word.split(" ")
for i in word:
result += chr(int(i) + 96)
elif type_of_cipher == "pair":
for i in word:
if i == "a":
result += "zb "
elif i == "z":
result += "ya "
else:
result += chr(ord(i)-1) + chr(ord(i)+1) + " "
elif type_of_cipher == "nokia":
for i in word:
num = ord(i)-96
num01 = num // 3
num02 = num % 3
if num02 != 0:
for i in range(num02):
result += str(num01+1)
else:
for i in range(3):
result += str(num01)
result += " "
elif type_of_cipher == "nokia02":
for letter in word:
result += nokia(letter)
result += " "
elif type_of_cipher == "snake":
result = squares(word)
return(result)
#snake
def nokia(letter):
sheet = [
[],["A", "B", "C"], ["D", "E","F"],
["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"],
["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"]
]
res = ""
for i in range(len(sheet)):
for n in range(len(sheet[i])):
if letter == sheet[i][n]:
for y in range(n+1):
res += str(i+1)
return res
def caesar(letter, num):
for i in letter:
if ord(i)+num > 122:
return chr(ord(i)+num - 26)
else:
return chr(ord(i)+num)
def to_morse(a, b, c, letter):
alphabet = {"a":a+b+c, "b":a+b+b+b+c, "c":b+a+b+a+c, "d":b+a+a+c, "e":a+c, "f":a+a+b+a+c, "g":b+b+a+c, "h":a+a+a+a+c, "i":a+a+c, "j":a+b+b+b+c, "k":b+a+b, "l":a+b+a+a+c, "m":b+b+c, "n":b+a+c, "o":b+b+b+c, "p":a+b+b+a+c, "q":b+b+a+b+c, "r":a+b+a+c, "s":a+a+a+c, "t":b+c, "u":a+a+b+c, "v":a+a+a+b+c, "w":a+b+b+c, "x":b+a+a+b+c, "y":b+a+b+b+c, "z":b+b+a+a+c}
if letter in alphabet.keys():
return alphabet[letter]
def to_morse_rnd(d, e, f, letter):
a = "a"
b = "b"
c = "c"
alphabet = {"a":a+b+c, "b":a+b+b+b+c, "c":b+a+b+a+c, "d":b+a+a+c, "e":a+c, "f":a+a+b+a+c, "g":b+b+a+c, "h":a+a+a+a+c, "i":a+a+c, "j":a+b+b+b+c, "k":b+a+b, "l":a+b+a+a+c, "m":b+b+c, "n":b+a+c, "o":b+b+b+c, "p":a+b+b+a+c, "q":b+b+a+b+c, "r":a+b+a+c, "s":a+a+a+c, "t":b+c, "u":a+a+b+c, "v":a+a+a+b+c, "w":a+b+b+c, "x":b+a+a+b+c, "y":b+a+b+b+c, "z":b+b+a+a+c}
res = ""
if letter in alphabet.keys():
for i in alphabet[letter]:
if i == a:
res += random.choice(d.split(","))
elif i == b:
res += random.choice(e.split(","))
elif i == c:
res += random.choice(f.split(","))
return res
def from_morse(inp):
alphabet = {'._': 'a', '.___': 'j', '_._.': 'c', '_..': 'd', '.': 'e', '.._.': 'f', '__.': 'g', '....': 'h', '..': 'i', '_._': 'k', '._..': 'l', '__': 'm', '_.': 'n', '___': 'o', '.__.': 'p', '__._': 'q', '._.': 'r', '...': 's', '_': 't', '.._': 'u', '..._': 'v', '.__': 'w', '_.._': 'x', '_.__': 'y', '__..': 'z'}
return alphabet[inp]
def squares(word):
result = ""
lenght = len(word)
side = False
if lenght < 100:
for i in range(10):
if lenght > i*i and lenght <= (i+1)*(i+1):
side = i+1
while len(word) != side*side:
word += chr(random.randint(65, 91))
square_l = snake(word, side)
for y in square_l:
for x in y:
result += x
result += " "
result += "\n"
return(result)
def snake(word, side):
output = []
for i in range(side):
l = []
for j in range(side):
l.append("_")
output.append(l)
smer = "r"
x = 0
y = 0
for i in word:
output[y][x] = i
if smer == "r":
if x == side-1:
smer = "d"
y += 1
elif output[y][x+1] != "_":
smer = "d"
y += 1
else:
x += 1
elif smer == "d":
if y == side-1:
smer = "l"
x -= 1
elif output[y+1][x] != "_":
smer = "l"
x -= 1
else:
y += 1
elif smer == "l":
if y == 0:
smer = "l"
y -= 1
elif output[y][x-1] != "_":
smer = "u"
y -= 1
else:
x -= 1
elif smer == "u":
if y == 0:
smer = "r"
x += 1
elif output[y-1][x] != "_":
smer = "r"
x += 1
else:
y -= 1
return(output)
#------------------------------------------------------------------------------------
inp = input("vstup: ")
while inp != "q":
print(tharkun_bot(inp))
inp = input("vstup: ")