-
Notifications
You must be signed in to change notification settings - Fork 4
/
commands.py
411 lines (322 loc) · 9.97 KB
/
commands.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import collections
import math
import fractions
import ast
from functools import reduce
letters = list("abcdefghijklmnopqrstuvwxyz")
numbers = list("0123456789")
def regular_arithmetic(a, b, operator):
if type(a) is list:
if type(b) is list:
result = []
for Q in range(0, len(a)):
result.append(eval("(" + str(ast.literal_eval(str(a[Q]))) + ")"
+ operator
+ "(" + str(ast.literal_eval(str(b[Q]))) + ")"))
return result
else:
result = []
for Q in a:
result.append(eval("(" + str(ast.literal_eval(str(Q))) + ")"
+ operator
+ "(" + str(ast.literal_eval(str(b))) + ")"))
return result
else:
if type(b) is list:
result = []
for Q in b:
result.append(eval("(" + str(ast.literal_eval(str(a))) + ")"
+ operator
+ "(" + str(ast.literal_eval(str(Q))) + ")"))
return result
else:
return eval("(" + str(ast.literal_eval(str(a))) + ")"
+ operator
+ "(" + str(ast.literal_eval(str(b))) + ")")
def single_arithmetic(a, operator):
if type(a) is list:
result = []
for Q in a:
result.append(eval(str(ast.literal_eval(str(Q))) + operator))
return result
else:
return eval(str(ast.literal_eval(str(a))) + operator)
def is_digit_value(value):
value = str(value)
try:
for X in value:
if numbers.__contains__(X):
continue
else:
return 0
return 1
except:
return 0
def largest_divisor(n):
if n == 2:
return 1
value = n // 2 + 1
while n % value != 0:
value -= 1
return value
def flatten(x):
if isinstance(x, collections.Iterable):
return [a for i in x for a in flatten(i)]
else:
return [x]
def deep_flatten(S):
if S == []:
return S
if isinstance(S[0], list):
return deep_flatten(S[0]) + deep_flatten(S[1:])
return S[:1] + deep_flatten(S[1:])
def is_alpha_value(value):
value = str(value)
try:
for X in value:
if letters.__contains__(X.lower()):
continue
else:
return 0
return 1
except:
return 0
def convert_to_base(n, base):
"""convert positive decimal integer n to equivalent in another base (2-36)"""
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~" \
"\u20AC\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160\u2039\u0152\u017D\u2018\u2019\u201C" \
"\u201D\u2013\u2014\u02DC\u2122\u0161\u203A\u0153\u017E\u0178\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6" \
"\u00A7\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7" \
"\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7" \
"\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7" \
"\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7" \
"\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7" \
"\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF"
if int(n) == 0:
return "0"
try:
n = int(n)
base = int(base)
except:
return ""
if n < 0 or base < 2 or base > 214:
if n > 0 and base == 1:
return "0" * n
return ""
s = ""
while 1:
r = n % base
s = digits[int(r)] + s
n = n // base
if n == 0:
break
try:
while s[0] == "0":
s = s[1:]
except:
0
return s
def convert_from_base(n, base):
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~" \
"\u20AC\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160\u2039\u0152\u017D\u2018\u2019\u201C" \
"\u201D\u2013\u2014\u02DC\u2122\u0161\u203A\u0153\u017E\u0178\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6" \
"\u00A7\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7" \
"\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7" \
"\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7" \
"\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7" \
"\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7" \
"\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF"
n = str(n)[::-1]
r = 0
range_v = 0
for Q in n:
r += digits.index(Q) * base ** range_v
range_v += 1
return r
def is_prime(n):
if type(n) is str:
n = ast.literal_eval(n)
if n == 2 or n == 3:
return 1
if n < 2 or n % 2 == 0 or n % 3 == 0:
return 0
if n < 9:
return 1
r = int(n ** 0.5)
f = 5
while f <= r:
if n % f == 0:
return 0
if n % (f + 2) == 0:
return 0
f += 6
return 1
def combinations(n, r):
n = int(n)
r = int(r)
if r > n:
return 0
return int(math.factorial(n) // (math.factorial(r) * math.factorial(n - r)))
def permutations(n, r):
n = int(n)
r = int(r)
if r > n:
return 0
return int(math.factorial(n) // math.factorial(n - r))
def prime_factorization(n):
n = int(n)
list_of_factors = []
if n < 2:
return []
else:
if n == 2:
return [2]
else:
for Q in range(2, n + 1):
if is_prime(Q):
if n % Q == 0:
list_of_factors.append(Q)
return list_of_factors
def get_letter(n):
n = int(n)
if n in range(1, 27):
return chr(64 + n)
return None
def prime_factorization_duplicates(n):
n = int(n)
list_of_factors = []
if n < 2:
return []
else:
if n == 2:
return [2]
else:
for Q in range(2, n + 1):
if is_prime(Q):
while n % Q == 0:
list_of_factors.append(Q)
n = int(int(n) // int(Q))
if n == 1: break
return list_of_factors
def prime_factorization_powers(n):
n = int(n)
list_of_factors = []
if n < 2:
return []
else:
if n == 2:
return [1]
else:
for Q in range(2, n + 1):
if is_prime(Q):
value = 0
while n % Q == 0:
value += 1
n = int(int(n) // int(Q))
list_of_factors.append(value)
try:
while list_of_factors[len(list_of_factors) - 1] == 0:
list_of_factors.pop()
except:0
return list_of_factors
def get_nth_prime(n):
n = int(n) + 1
current_prime = 2
while n > 0:
if is_prime(current_prime):
n -= 1
if n > 0:
current_prime += 1
else:
current_prime += 1
return current_prime
def get_all_substrings(input_string):
length = len(input_string)
return [input_string[i:j+1] for i in range(length) for j in range(i, length)]
def command_gcd(numbers):
if 0 in numbers:
return 0
try:
return reduce(fractions.gcd, numbers)
except:
return 1
def command_lcm(numbers):
if 0 in numbers:
return 0
try:
return reduce(lcm, numbers)
except:
return 1
def lcm(a, b):
if a == 0 or b == 0:
return 0
return abs(a) * abs(b) // command_gcd([a, b])
def floatify(string):
a = str(string)
is_neg = False
if a[0] == "-":
is_neg = True
a = a[1:]
if not str(a).__contains__("."):
a += "."
while a[0] == "0":
a = a[1:]
while a[-1] == "0":
a = a[0:-1]
if a[0] == ".":
a = "0" + a
if a[-1] == ".":
a += "0"
if is_neg:
a = "-" + a
return a
def trim_float(string):
if str(string)[-2:] == ".0":
return int(string)
else:
return floatify(string)
def is_float_value(string):
number_of_dots = str(string).count(".")
string = str(string).replace(".", "")
if is_digit_value(string) and number_of_dots < 2:
return 1
else:
return 0
def euler_totient(n):
amount = 0
if is_prime(n):
return n - 1
for k in range(1, n + 1):
if fractions.gcd(n, k) == 1:
amount += 1
return amount
def chunk_divide(seq, num):
if type(seq) is int:
seq = str(seq)
seq = seq[::-1]
avg = len(seq) / float(num)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)][::-1])
last += avg
return out[::-1]
def minimum_edit_distance(s1,s2):
s1 = str(s1)
s2 = str(s2)
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = range(len(s1) + 1)
for index2,char2 in enumerate(s2):
newDistances = [index2 + 1]
for index1, char1 in enumerate(s1):
if char1 == char2:
newDistances.append(distances[index1])
else:
newDistances.append(1 + min((distances[index1],
distances[index1 + 1],
newDistances[-1])))
distances = newDistances
return distances[-1]
def string_partitions(string):
length = len(string)