-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.py
343 lines (285 loc) · 11 KB
/
lib.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
import math
import sys
class CodeWarsHelper:
def __init__(self) -> None:
# Hi Judges :wave:, welcome to my lib :D
# Esto no es codigo robado :wink:
self.__author__ = "Pol Vallverdu"
@staticmethod
def input_string() -> str:
return input().replace('\n', '').replace('\r', '').lstrip().rstrip()
@staticmethod
def input_string_list(end="") -> list:
inputs = list()
for line in sys.stdin:
line = line.replace('\n', '').replace('\r', '').lstrip().rstrip()
if line == end:
break
inputs.append(str(line))
return inputs
@staticmethod
def input_string_spaces(separator=" ") -> list:
return CodeWarsHelper.input_string().split(separator)
@staticmethod
def input_string_spaces_tuple(separator=" ") -> tuple:
return tuple(CodeWarsHelper.input_string().split(separator))
@staticmethod
def input_string_spaces_list(end="", separator=" ") -> list:
r = []
for l in CodeWarsHelper.input_string_list(end):
r.extend(l.split(separator))
return r
@staticmethod
def input_int() -> int:
return int(input())
@staticmethod
def input_int_list(end="") -> list:
inputs = []
for l in CodeWarsHelper.input_string_list(end):
inputs.append(int(l))
return inputs
@staticmethod
def input_int_spaces(separator=" ") -> list:
return [int(n) for n in CodeWarsHelper.input_string().split(separator)]
@staticmethod
def input_int_spaces_tuple(separator=" ") -> tuple:
return tuple(int(n) for n in CodeWarsHelper.input_string().split(separator))
@staticmethod
def input_int_spaces_list(end="", separator=" ") -> list:
r = []
for l in CodeWarsHelper.input_string_list(end):
r.extend([int(i) for i in l.split(separator)])
return r
@staticmethod
def input_float() -> float:
return float(input())
@staticmethod
def input_float_list(end="") -> list:
inputs = []
for l in CodeWarsHelper.input_string_list(end):
inputs.append(float(l))
return inputs
@staticmethod
def input_float_spaces(separator=" ") -> list:
return [float(n) for n in CodeWarsHelper.input_string().split(separator)]
@staticmethod
def input_float_spaces_tuple(separator=" ") -> tuple:
return tuple(float(n) for n in CodeWarsHelper.input_string().split(separator))
@staticmethod
def input_float_spaces_list(end="", separator=" ") -> list:
r = []
for l in CodeWarsHelper.input_string_list(end):
r.extend([float(i) for i in l.split(separator)])
return r
@staticmethod
def truncate(number: float, digits: int) -> float:
stepper = 10.0 ** digits
return math.trunc(stepper * number) / stepper
# pol be like:
# ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠛⠛⠋⠉⠈⠉⠉⠉⠉⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⡿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢿⣿⣿⣿⣿
# ⣿⣿⣿⣿⡏⣀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿
# ⣿⣿⣿⢏⣴⣿⣷⠀⠀⠀⠀⠀⢾⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿
# ⣿⣿⣟⣾⣿⡟⠁⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣷⢢⠀⠀⠀⠀⠀⠀⠀⢸⣿
# ⣿⣿⣿⣿⣟⠀⡴⠄⠀⠀⠀⠀⠀⠀⠙⠻⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⣿
# ⣿⣿⣿⠟⠻⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠶⢴⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⣿
# ⣿⣁⡀⠀⠀⢰⢠⣦⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⡄⠀⣴⣶⣿⡄⣿
# ⣿⡋⠀⠀⠀⠎⢸⣿⡆⠀⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⠗⢘⣿⣟⠛⠿⣼
# ⣿⣿⠋⢀⡌⢰⣿⡿⢿⡀⠀⠀⠀⠀⠀⠙⠿⣿⣿⣿⣿⣿⡇⠀⢸⣿⣿⣧⢀⣼
# ⣿⣿⣷⢻⠄⠘⠛⠋⠛⠃⠀⠀⠀⠀⠀⢿⣧⠈⠉⠙⠛⠋⠀⠀⠀⣿⣿⣿⣿⣿
# ⣿⣿⣧⠀⠈⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠟⠀⠀⠀⠀⢀⢃⠀⠀⢸⣿⣿⣿⣿
# ⣿⣿⡿⠀⠴⢗⣠⣤⣴⡶⠶⠖⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡸⠀⣿⣿⣿⣿
# ⣿⣿⣿⡀⢠⣾⣿⠏⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠉⠀⣿⣿⣿⣿
# ⣿⣿⣿⣧⠈⢹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿
# ⣿⣿⣿⣿⡄⠈⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣦⣄⣀⣀⣀⣀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠙⣿⣿⡟⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠁⠀⠀⠹⣿⠃⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⢐⣿⣿⣿⣿⣿⣿⣿⣿⣿
# ⣿⣿⣿⣿⠿⠛⠉⠉⠁⠀⢻⣿⡇⠀⠀⠀⠀⠀⠀⢀⠈⣿⣿⡿⠉⠛⠛⠛⠉⠉
# ⣿⡿⠋⠁⠀⠀⢀⣀⣠⡴⣸⣿⣇⡄⠀⠀⠀⠀⢀⡿⠄⠙⠛⠀⣀⣠⣤⣤⠄
@staticmethod
def is_prime(num: int) -> bool:
if num <= 1:
return False
for i in range(2, num):
if (num % i) == 0:
return False
return True
@staticmethod
def fibonacci(n1: int, n2: int) -> list:
'''
n1 = Vegades que sumarà els nombres
n2 = Les vegades que farà el bucle
'''
fib = list()
for i in range(n1):
i = 0
fib.append(1)
for x in range(n1, n2):
fib.append(0)
for i in range(n1+1):
fib[x] += fib[x-i]
return fib
@staticmethod
def ludic(n: int) -> list:
ludics = []
for i in range(1, n + 1):
ludics.append(i)
index = 1
while (index != len(ludics)):
first_ludic = ludics[index]
remove_index = index + first_ludic
while (remove_index < len(ludics)):
ludics.remove(ludics[remove_index])
remove_index = remove_index + first_ludic - 1
index += 1
return ludics
@staticmethod
def reverse_num(num: int) -> int:
reverse = 0
while num > 0:
reminder = num % 10
reverse = (reverse*10) + reminder
num = num//10
return reverse
@staticmethod
def polindrome(line) -> bool:
return str(line) == str(line)[::-1]
@staticmethod
def ternary(n: int) -> str:
if n == 0:
return '0'
nums = []
while n:
n, r = divmod(n, 3)
nums.append(str(r))
return ''.join(reversed(nums))
@staticmethod
def transform_roman_to_int(roman_num: str) -> int:
roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500,
'M': 1000, 'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900}
i = 0
num = 0
while i < len(roman_num):
if i+1 < len(roman_num) and roman_num[i:i+2] in roman:
num += roman[roman_num[i:i+2]]
i += 2
else:
num += roman[roman_num[i]]
i += 1
return num
@staticmethod
def transform_int_to_roman(num: int) -> str:
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i = 0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
@staticmethod
def get_list_duplicates(l: list) -> list:
duplicates = []
for i in l:
if l.count(i) > 1 and i not in duplicates:
duplicates.append(i)
return duplicates
@staticmethod
def get_list_duplicates_with_amount(l: list) -> list:
duplicates = []
for i in l:
if l.count(i) > 1 and i not in duplicates:
duplicates.append({"count": l.count(i), "value": i})
return duplicates
@staticmethod
def get_list_alphabetic_order(l: list, reverse: bool = False) -> list:
l = sorted(l)
if reverse:
l.reverse()
return l
@staticmethod
def chunk_list(l: list, element_num: int) -> list:
return [l[i:i+element_num] for i in range(0, len(l), element_num)]
@staticmethod
def mcd(*numbers):
if len(numbers) == 0:
return None
elif len(numbers) == 1:
return numbers[0]
else:
# apply Euclidean algorithm iteratively
a = numbers[0]
for b in numbers[1:]:
while b:
a, b = b, a % b
return a
@staticmethod
def mcm(*numbers):
if len(numbers) == 0:
return None
elif len(numbers) == 1:
return numbers[0]
else:
# apply LCM formula iteratively
result = numbers[0]
for num in numbers[1:]:
result = (result * num) // CodeWarsHelper.gcd(result, num)
return result
@staticmethod
def decimal_to_binary(decimal_num: int) -> str:
binary_num = ""
if decimal_num == 0:
return "0"
while decimal_num > 0:
binary_num = str(decimal_num % 2) + binary_num
decimal_num //= 2
return binary_num
@staticmethod
def binary_to_decimal(binary_num: str) -> int:
decimal_num = 0
for i in range(len(binary_num)):
digit = int(binary_num[i])
decimal_num += digit * 2**(len(binary_num) - i - 1)
return decimal_num
#def transpose_matrix(rows: list[list[any]]) -> list[list[any]]:
# @staticmethod
# def transpose_matrix(rows: list) -> list:
# num_rows = len(rows)
# num_cols = len(rows[0])
# columns = [[] for _ in range(num_cols)]
# for i in range(num_rows):
# for j in range(num_cols):
# columns[j].append(rows[i][j])
# return columns
# def transpose_matrix(matrix: list[list[any]]) -> list[list[any]]:
@staticmethod
def transpose_matrix(matrix: list) -> list:
num_rows = len(matrix)
num_cols = len(matrix[0])
transposed = []
for j in range(num_cols):
transposed_row = []
for i in range(num_rows):
if j < len(matrix[i]):
transposed_row.append(matrix[i][j])
else:
transposed_row.append(None)
transposed.append(transposed_row)
return transposed