-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
832 lines (667 loc) · 21.7 KB
/
main.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
import random
import string
from typing import List, Tuple, Optional, Iterator, Generator
from collections import deque, defaultdict
import hashlib
import operator
from collections import Counter
import math
import sys
def bubble_sort(numbers: List[int]) -> List[int]:
len_numbers = len(numbers)
for i in range(len_numbers):
for j in range(len_numbers - 1 - i):
if numbers[j] > numbers[j+1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
return numbers
def linear_search(numbers: List[int], value: int) -> int:
for i in range(len(numbers)):
if numbers[i] == value:
return i
return -1
def binary_search(numbers: List[int], value: int) -> int:
left, right = 0, len(numbers) - 1
while left <= right:
mid = (left + right) // 2
if numbers[mid] == value:
return mid
elif numbers[mid] < value:
left = mid + 1
else:
right = mid - 1
return - 1
def binary_search_2(numbers: List[int], value: int) -> int:
def _binary_search(numbers: List[int], value: int,
left: int, right: int) -> int:
if left > right:
return - 1
mid = (left + right) // 2
if numbers[mid] == value:
return mid
elif numbers[mid] < value:
return _binary_search(numbers, value, mid + 1, right)
else:
_binary_search(numbers, value, left, mid - 1)
_binary_search(numbers, value, 0, len(numbers)-1)
# class Node(object):
# def __init__(self, data, next_node=None):
# self.data = data
# self.next = next_node
class LinkedList(object):
def __init__(self, head=None) -> None:
self.head = head
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def insert(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
class HashTable(object):
def __init__(self, size=10) -> None:
self.size = size
self.table = [[] for i in range(self.size)]
def hash(self, key) -> int:
return int(hashlib.md5(key.encode()).hexdigest(), base=16) % self.size
def add(self, key, value) -> None:
index = self.hash(key)
for data in self.table[index]:
if data[0] == key:
data[1] = value
break
else:
self.table[index].append([key, value])
def print(self) -> None:
for index in range(self.size):
print(index, end=" ")
for data in self.table[index]:
print("-->", end=" ")
print(data, end=" ")
print()
def get(self, key):
index = self.hash(key)
for data in self.table[index]:
if data[0] == key:
return data[1]
def __setitem__(self, key, value) -> None:
self.add(key, value)
def __getitem__(self, key):
return self.get(key)
def getPair(numbers: List[int], target: int) -> Optional[Tuple[int, int]]:
cache = set()
for num in numbers:
val = target - num
if val in cache:
return val, num
cache.add(num)
def getPair_half_sum(numbers: List[int]) -> Optional[Tuple[int, int]]:
sum_numbers = sum(numbers)
# if sum_numbers % 2 != 0:
# return
# half_sum = int(sum_numbers / 2)
half_sum, remainder = divmod(sum_numbers, 2)
if remainder != 0:
return
cache = set()
for num in numbers:
cache.add(num)
val = half_sum - num
if val in cache:
return val, num
class Stack(object):
def __init__(self) -> None:
self.stack = []
def push(self, data) -> None:
self.stack.append(data)
def pop(self):
if self.stack:
return self.stack.pop()
def validate_format(chars: str) -> bool:
lookup = {"{": "}", "[": "]", "(": ")"}
stack = []
for c in chars:
if c in lookup.keys():
stack.append(lookup[c])
if c in lookup.values():
if not stack:
return False
if c != stack.pop():
return False
if stack:
return False
return True
class Que(object):
def __init__(self):
self.queue = []
def enqueue(self, data):
self.queue.append(data)
def dequeue(self):
if self.queue:
return self.queue.pop(0)
def reverse(queue):
new_queue = deque()
while queue:
new_queue.append(queue.pop())
return new_queue
class Node(object):
def __init__(self, value: int) -> None:
self.value = value
self.left = None
self.right = None
def insert(node: Node, value: int) -> Node:
if node is None:
return Node(value)
if value < node.value:
node.left = insert(node.left, value)
else:
node.right = insert(node.right, value)
return node
def find_pair(pairs: List[Tuple[int, int]]) -> Iterator[Tuple[int, int]]:
cache = {}
for pair in pairs:
first, second = pair[0], pair[1]
value = cache.get(second)
if not value:
cache[first] = second
elif value == first:
yield pair
def find_max_value(value: str):
cache = []
for a in value:
if a.isalpha():
cache.append(a.lower())
List = Counter(cache).most_common()
print(cache)
return List[0]
def count_chars_v1(strings: str) -> Tuple[str, int]:
strings = strings.lower()
# l = []
# for char in strings:
# if not char.isspace():
# l.append((char, strings.count(char)))
ln = [(c, strings.count(c)) for c in strings if not c.isspace()]
return max(ln, key=operator.itemgetter(1))
def count_chars_v2(strings: str) -> Tuple[str, int]:
strings = strings.lower()
d = {}
for char in strings:
if not char.isspace():
d[char] = d.get(char, 0) + 1
max_key = max(d, key=d.get)
return max_key, d[max_key]
def count_chars_v3(strings: str) -> Tuple[str, int]:
strings = strings.lower()
d = Counter()
for char in strings:
print(d)
if not char.isspace():
d[char] += 1
max_key = max(d, key=d.get)
print(d)
return max_key, d[max_key]
def memoize(f):
cache = {}
def _wrapper(n):
if n not in cache:
cache[n] = f(n)
return cache[n]
return _wrapper
# @memoize
def long_func(num: int) -> int:
r = 0
for i in range(10000000):
r += num * i
return r
def min_count_remove(x: List[int], y: List[int]) -> None:
# count_x = {}
# count_y = {}
# for i in x:
# count_x[i] = count_x.get(i, 0) + 1
# for i in y:
# count_y[i] = count_y.get(i, 0) + 1
counter_x = Counter(x)
counter_y = Counter(y)
for key_x, value_x in counter_x.items():
value_y = counter_y.get(key_x)
if value_y:
if value_x < value_y:
x[:] = [i for i in x if i != key_x]
elif value_x > value_y:
y[:] = [i for i in y if i != key_x]
def remove_zero(numbers: List[int]) -> None:
if numbers and numbers[0] == 0:
numbers.pop(0)
remove_zero(numbers)
def list_to_int(numbers: List[int]) -> int:
sum_numbers = 0
for i, num in enumerate(reversed(numbers)):
sum_numbers += num * (10 ** i)
return sum_numbers
def list_to_int_plus_one(numbers: List[int]) -> int:
i = len(numbers) - 1
numbers[i] += 1
while 0 < i:
if numbers[i] != 10:
remove_zero(numbers)
break
numbers[i] = 0
numbers[i-1] += 1
i -= 1
else:
if numbers[0] == 10:
numbers[0] = 1
numbers.append(0)
return list_to_int(numbers)
def snake_string_v1(chars: str) -> List[List[str]]:
result = [[], [], []]
result_indexes = {0, 1, 2}
insert_index = 1
for i, s in enumerate(chars):
if i % 4 == 1:
insert_index = 0
elif i % 2 == 0:
insert_index = 1
elif i % 4 == 3:
insert_index = 2
result[insert_index].append(s)
for rest_index in result_indexes - {insert_index}:
result[rest_index].append(" ")
return result
def snake_string_v2(chars: str, depth: int) -> List[List[str]]:
result = [[] for _ in range(depth)]
result_indexes = {i for i in range(depth)}
insert_index = int(depth / 2)
# def pos(i):
# return i + 1
# def neg(i):
# return i - 1
op = operator.neg
for s in chars:
result[insert_index].append(s)
for rest_index in result_indexes - {insert_index}:
result[rest_index].append(" ")
if insert_index <= 0:
op = operator.pos
if insert_index >= depth - 1:
op = operator.neg
insert_index += op(1)
return result
def get_max_sequence_sum(numbers: List[int]) -> int:
result_sequence, sum_sequence = 0, 0
for num in numbers:
# temp_sum_sequence = sum_sequence + num
# if num < temp_sum_sequence:
# sum_sequence = temp_sum_sequence
# else:
# sum_sequence = num
sum_sequence = max(num, sum_sequence + num)
# if result_sequence < sum_sequence:
# result_sequence = sum_sequence
result_sequence = max(result_sequence, sum_sequence)
return result_sequence
def find_max_circular_sequence(numbers: List[int]) -> int:
max_sequence_sum = get_max_sequence_sum(numbers)
invert_numbers = []
all_sum = 0
for num in numbers:
all_sum += num
invert_numbers.append(-num)
max_wrap_sequence = all_sum-(-get_max_sequence_sum(invert_numbers))
return max(max_sequence_sum, max_wrap_sequence)
def delete_duplicate_v1(numbers: List[int]) -> None:
tmp = []
for num in numbers:
if num not in tmp:
tmp.append(num)
numbers[:] = tmp
print(numbers)
def delete_duplicate_v2(numbers: List[int]) -> None:
tmp = [numbers[0]]
i, len_num = 0, len(numbers) - 1
while i < len_num:
if numbers[i] != numbers[i + 1]:
tmp.append(numbers[i + 1])
i += 1
numbers[:] = tmp
print(numbers)
def delete_duplicate_v3(numbers: List[int]) -> None:
i, len_numbers = 0, len(numbers) - 1
while i < len_numbers - 1:
if numbers[i] == numbers[i + 1]:
numbers.remove((numbers[i]))
i -= 1
i += 1
def delete_duplicate_v4(numbers: List[int]) -> None:
i = len(numbers) - 1
while i > 0:
if numbers[i] == numbers[i - 1]:
numbers.pop(i)
i -= 1
def all_perms(elements: List[int]) -> Iterator[List[int]]:
if len(elements) <= 1:
yield elements
else:
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
yield perm[:i] + elements[0:1] + perm[i:]
def is_palindrome(strings: str) -> bool:
len_strings = len(strings)
if not len_strings:
return False
if len_strings == 1:
return True
start, end = 0, len_strings-1
while start < end:
if strings[start] != strings[end]:
return False
start += 1
end -= 1
return True
def find_palindrome(strings: str, left: int, right: int):
# result = []
while 0 <= left and right <= len(strings) - 1:
if strings[left] != strings[right]:
break
yield strings[left: right + 1]
left -= 1
right += 1
def find__all_palindrome(strings: str):
# result = []
len_strings = len(strings)
if not len_strings:
yield
if len_strings == 1:
yield strings
for i in range(1, len_strings - 1):
yield from find_palindrome(strings, i - 1, i + 1)
yield from find_palindrome(strings, i - 1, i)
def order_even_odd_last(numbers: List[int]) -> None:
even_list = []
odd_list = []
for num in numbers:
if num % 2 == 0:
even_list.append(num)
else:
odd_list.append(num)
numbers[:] = even_list+odd_list
def order_even_odd_last_v2(numbers: List[int]) -> None:
i, j = 0, len(numbers) - 1
while i < j:
if numbers[i] % 2 == 0:
i += 1
else:
numbers[i], numbers[j] = numbers[j], numbers[i]
j -= 1
def order_change_index_v1(chars: List[str], indexes: List[int]) -> str:
tmp = [None] * len(chars)
for i, index in enumerate(indexes):
tmp[index] = chars[i]
return "".join(tmp)
def order_change_index_v2(chars: List[str], indexes: List[int]) -> str:
i, len_indexes = 0, len(indexes) - 1
while i < len_indexes:
while i != indexes[i]:
index = indexes[i]
chars[index], chars[i] = chars[i], chars[index]
indexes[index], indexes[i] = indexes[i], indexes[index]
i += 1
return "".join(chars)
NUM_ALPHABET_MAPPING = {
0: "+",
1: "@",
2: "ABC",
3: "DEF",
4: "GHI",
5: "JKL",
6: "MNO",
7: "PQRS",
8: "TUV",
9: "WXYZ",
}
def phone_mnemonic_v1(phone_numbers: str) -> List[str]:
phone_number = [int(s) for s in phone_numbers.replace("-", "")]
candidate = []
tmp = [""] * len(phone_number)
def find_candidate_alphabet(digit: int = 0) -> None:
if digit == len(phone_number):
candidate.append("".join(tmp))
else:
for char in NUM_ALPHABET_MAPPING[phone_number[digit]]:
tmp[digit] = char
find_candidate_alphabet(digit + 1)
find_candidate_alphabet()
return candidate
def generate_prime_v1(number: int) -> List[int]:
primes = []
for x in range(2, number + 1):
for y in range(2, x):
if x % y == 0:
break
else:
primes.append(x)
return primes
def generate_prime_v2(number: int) -> List[int]:
primes = []
cache = {}
for x in range(2, number + 1):
is_prime = cache.get(x)
if is_prime is False:
continue
primes.append(x)
cache[x] = True
for y in range(x ** 2, number + 1, x):
cache[y] = False
return primes
def generate_prime_v3(number: int) -> Generator[int, None, None]:
cache = {}
for x in range(2, number + 1):
is_prime = cache.get(x)
if is_prime is False:
continue
yield x
cache[x] = True
for y in range(x ** 2, number + 1, x):
cache[y] = False
def is_prime_v1(num: int) -> bool:
if num <= 1:
return False
for i in range(2, num):
if num % i == 0:
return False
return True
def is_prime_v2(num: int) -> bool:
if num <= 1:
return False
# for i in range(2, math.floor(math.sqrt(num)) + 1):
# if num % i == 0:
# return False
i = 2
while i * i <= num:
if num % i == 0:
return False
i += 1
return True
def is_prime_v3(num: int) -> bool:
if num <= 1:
return False
if num == 2:
return True
if num % 2 == 0:
return False
for i in range(3, math.floor(math.sqrt(num) + 1), 2):
if num % i == 0:
return False
return True
def is_prime_v4(num: int) -> bool:
if num <= 1:
return False
if num <= 3:
return True
if num % 2 == 0 or num % 3 == 0:
return False
for i in range(5, math.floor(math.sqrt(num)) + 1, 6):
if num % i == 0 or num % (i + 2) == 0:
return False
return True
def taxi_cab_number(max_answer_num: int, match_answer_num: int = 2) -> List[Tuple[int, List[Tuple[int, int]]]]:
result = []
got_answer_count = 0
answer = 1
while got_answer_count < max_answer_num:
match_answer_count = 0
memo = defaultdict(list)
max_param = int(pow(answer, 1.0 / 3)) + 1
for x in range(1, max_param):
for y in range(x + 1, max_param):
if x ** 3 + y ** 3 == answer:
match_answer_count += 1
memo[answer].append((x, y))
if match_answer_count == max_answer_num:
result.append((answer, memo[answer]))
got_answer_count += 1
answer += 1
return result
def felmer_last_theorem_v1(max_num: int, square_num: int) -> List[Tuple[int, int]]:
result = []
if square_num < 2:
return result
max_z = int(pow((max_num-1)**2+max_num**2, 1.0/square_num))
for x in range(1, max_num + 1):
for y in range(x + 1, max_num + 1):
for z in range(y + 1, max_z):
if pow(x, square_num) + pow(y, square_num) == pow(z, square_num):
result.append((x, y, z))
return result
def felmer_last_theorem_v2(max_num: int, square_num: int) -> List[Tuple[int, int]]:
result = []
if square_num < 2:
return result
for x in range(1, max_num + 1):
for y in range(x + 1, max_num + 1):
pow_sum = pow(x, square_num)+pow(y, square_num)
if pow_sum > sys.maxsize:
raise ValueError(x, y, z, square_num, pow_sum)
z = pow(pow_sum, 1.0 / square_num)
if not z.is_integer():
continue
z = int(z)
z_pow = pow(z, square_num)
if z_pow == pow_sum:
result.append((x, y, z))
return result
def ceaser_cipher(text: str, shift: int) -> str:
result = ""
len_alphabet = ord("Z")-ord("A")+1
for char in text:
# if char.isupper():
# alphabet = string.ascii_uppercase
# elif char.islower():
# alphabet = string.ascii_lowercase
# else:
# result += char
# continue
# index = (alphabet.index(char)+shift) % len(alphabet)
# result += alphabet[index]
if char.isupper():
result += chr((ord(char)+shift-ord("A")) % len_alphabet+ord("A"))
elif char.islower():
result += chr((ord(char)+shift-ord("a")) % len_alphabet+ord("a"))
else:
result += char
return result
def caeser_cipher_hack(text: str) -> Generator[Tuple[int, str], None, None]:
len_alphabet = ord("Z") - ord("A") + 1
# len_alphabet = len(string.ascii_uppercase)
for candidate_shift in range(1, len_alphabet + 1):
reverted = ""
for char in text:
# if char.isupper():
# alphabet = string.ascii_uppercase
# elif char.islower():
# alphabet = string.ascii_lowercase
# else:
# reverted += char
# continue
# index = alphabet.index(char) - candidate_shift
# if index < 0:
# index += len_alphabet
# reverted += alphabet[index]
if char.isupper():
index = ord(char) - candidate_shift
if index < ord("A"):
index += chr(index)
reverted += chr(index)
elif char.islower():
index = ord(char) - candidate_shift
if index < ord("a"):
index += len_alphabet
reverted += chr(index)
else:
reverted += char
yield candidate_shift, reverted
def hanoi(disk: int, src: str, dest: str, support: str):
if disk < 1:
return
hanoi(disk-1, src, support, dest)
print(f'move {disk} from {src} to {dest}')
hanoi(disk-1, support, dest, src)
def get_hanoi_movement(disk: int, src: str, dest: str, support: str) -> List[Tuple[int, str]]:
result = []
def _hanoi(disk: int, src: str, dest: str, support: str):
if disk < 1:
return
_hanoi(disk-1, src, support, dest)
result.append((disk, src, dest))
_hanoi(disk - 1, support, dest, src)
_hanoi(disk, src, dest, support)
return result
def generate_pascal_triangle(depth: int) -> List[List[int]]:
data = [[1] * (i + 1) for i in range(depth)]
for line in range(2, depth):
for i in range(1, line):
data[line][i] = data[line - 1][i - 1] + data[line - 1][i]
return data
def print_pascal(data: List[int]) -> None:
max_digit = len(str(max(data[-1])))
width = max_digit+(max_digit % 2)+2
for i, line in enumerate(data):
numbers = "".join([str(i).center(width, " ") for i in line])
print((" " * int(width/2))*(len(data)-i), numbers)
def generate_triangle_list(depth: int, max_num: int) -> List[List[int]]:
return [[random.randint(0, max_num) for _ in range(i)]for i in range(1, depth+1)]
def print_triangle(data: List[List[int]]) -> None:
max_digit = len(str(max([max(l) for l in data])))
width = max_digit + (max_digit % 2) + 2
for index, line in enumerate(data):
numbers = "".join([str(i).center(width, " ") for i in line])
print(" " * int(width/2)*(len(data)-index), numbers)
def sum_min_path(triangle: List[List[int]]) -> Optional[int]:
tree_sum = triangle[:]
j, len_triangle = 1, len(triangle)
if not len_triangle:
return
while j < len_triangle:
line = triangle[j]
line_path_sum = []
for i, value in enumerate(line):
if i == 0:
sum_value = line[i]+tree_sum[j-1][0]
elif i == len(line) - 1:
sum_value = line[i]+tree_sum[j-1][i-1]
else:
min_path = min(tree_sum[j-1][i-1], min([tree_sum[j-1][i]]))
sum_value = line[i]+min_path
line_path_sum.append(sum_value)
tree_sum[j] = line_path_sum
j += 1
return (min(tree_sum[-1]))
if __name__ == '__main__':
data = generate_triangle_list(5, 9)
print_triangle(data)
print('min_path -->>', sum_min_path(data))