Skip to content

Commit 0cf33d1

Browse files
committed
Use abstract container types as type hints in function's parameters
1 parent 86e07f3 commit 0cf33d1

File tree

23 files changed

+78
-57
lines changed

23 files changed

+78
-57
lines changed

python/euler/bin/p0018.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# project euler: problem 18
22

3-
from collections.abc import Callable
3+
from collections.abc import Callable, Reversible, Sequence
44
from functools import reduce
55
from itertools import pairwise
66
from operator import add
@@ -26,7 +26,7 @@
2626
# fmt: on
2727

2828

29-
def compute(fn: Callable[..., int], nums: list[list[int]]) -> str:
29+
def compute(fn: Callable[..., int], nums: Reversible[Sequence[int]]) -> str:
3030
return str(reduce(lambda x, y: list(map(add, map(fn, pairwise(x)), y)), reversed(nums))[0])
3131

3232

python/euler/bin/p0022.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# project euler: problem 22
22

3+
from collections.abc import Iterable
34
from typing import IO
45

56

6-
def total_score(words: list[str]) -> int:
7+
def total_score(words: Iterable[str]) -> int:
78
def score(word):
89
return sum(ord(c) - (ord('A') - 1) for c in word)
910

python/euler/bin/p0024.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# project euler: problem 24
22

3+
from collections.abc import MutableSequence
34
from math import factorial
45

56

@@ -14,7 +15,7 @@ def make_fact_tbl(num: int, depth: int) -> list[int]:
1415
return result
1516

1617

17-
def compute(nth: int, elm_lst: list[int], depth: int) -> str:
18+
def compute(nth: int, elm_lst: MutableSequence[int], depth: int) -> str:
1819
idx = nth - 1
1920
result = []
2021
for n in make_fact_tbl(len(elm_lst), depth):

python/euler/bin/p0031.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# project euler: problem 31
22

3+
from collections.abc import Iterable
34

4-
def compute(coins: list[int], target: int) -> str:
5+
6+
def compute(coins: Iterable[int], target: int) -> str:
57
tbl = [0] * (target + 1)
68
tbl[0] = 1
79

python/euler/bin/p0033.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
#
3333
# I only need to search in the case #1.
3434

35-
from collections.abc import Iterator
35+
from collections.abc import Generator
3636
from functools import reduce
3737
from itertools import combinations
3838
from math import gcd
3939

4040

41-
def make_cands() -> Iterator[tuple[int, int]]:
41+
def make_cands() -> Generator[tuple[int, int], None, None]:
4242
# Note: The function check() assumes that a < b < c.
4343
#
4444
# The combination tuples which are generated by itertools.combinations() are emitted

python/euler/bin/p0037.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
# candidate numbers: [2357][1379]*[37] (n >= 10)
44
# ----------- lst
55

6+
from collections.abc import Iterable
7+
68
from euler.lib.prime import is_prime
79
from euler.lib.util import num_of_digits
810

911

10-
def add_prefix_num(pre_lst: list[int], lst: list[int]) -> list[int]:
12+
def add_prefix_num(pre_lst: Iterable[int], lst: Iterable[int]) -> list[int]:
1113
return [p * (10 ** num_of_digits(n)) + n for p in pre_lst for n in lst]
1214

1315

14-
def make_next_lists(lst: list[int]) -> list[int]:
16+
def make_next_lists(lst: Iterable[int]) -> list[int]:
1517
return list(filter(is_prime, add_prefix_num([1, 3, 7, 9], lst)))
1618

1719

18-
def pickup_primes(lst: list[int]) -> list[int]:
20+
def pickup_primes(lst: Iterable[int]) -> list[int]:
1921
def is_truncable_prime(n: int) -> bool:
2022
if n == 0:
2123
return False

python/euler/bin/p0042.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# project euler: problem 42
22

3+
from collections.abc import Iterable
34
from typing import IO
45

56
from euler.lib.util import is_triangular
67

78

8-
def calc_scores(words: list[str]) -> list[int]:
9+
def calc_scores(words: Iterable[str]) -> list[int]:
910
a_to_z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1011
tbl = dict(zip(a_to_z, range(1, len(a_to_z) + 1), strict=True))
1112

python/euler/bin/p0050.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# project euler: problem 50
22

3-
from collections.abc import Iterator
3+
from collections.abc import Generator
44
from itertools import dropwhile
55

66
from euler.lib.prime import is_prime, prime_generator
77

88

9-
def cumsum_generator() -> Iterator[int]:
9+
def cumsum_generator() -> Generator[int, None, None]:
1010
acc = 0
1111
p_gen = prime_generator()
1212
while True:
@@ -17,7 +17,7 @@ def cumsum_generator() -> Iterator[int]:
1717
# Returns a cumulative sum list of prime numbers.
1818
# [0, p1, p1+p2, p1+p2+p3, ..., p1+...+p{n-1}, p1+...+p{n-1}+p{n}]
1919
# where sum(p1..p{n-1}) < limit and sum(p1..p{n}) >= limit
20-
def init_cumsum_lst(cs_gen: Iterator[int], limit: int) -> list[int]:
20+
def init_cumsum_lst(cs_gen: Generator[int, None, None], limit: int) -> list[int]:
2121
lst = [0]
2222
while lst[-1] < limit:
2323
lst.append(next(cs_gen))

python/euler/bin/p0051.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#
3434
# 4) There are at least same three numbers other than last digit.
3535

36+
from collections.abc import Iterable, Sequence
3637
from functools import reduce
3738
from itertools import combinations, count
3839

@@ -85,7 +86,7 @@ def flip_bits(pos_tpl: tuple[int, tuple[int, ...]]) -> tuple[int, list[int]]:
8586

8687
# assemble_num(231, [0, 1, 1, 1, 0, 0], 7)
8788
# --> 237771
88-
def assemble_num(i: int, pat: list[int], r: int) -> int:
89+
def assemble_num(i: int, pat: Iterable[int], r: int) -> int:
8990
lst = []
9091
for flag in pat:
9192
if flag == 0:
@@ -97,7 +98,7 @@ def assemble_num(i: int, pat: list[int], r: int) -> int:
9798
return reduce(lambda x, y: 10 * x + y, reversed(lst))
9899

99100

100-
def is_probable(i: int, pat: list[int], current_min: int) -> bool:
101+
def is_probable(i: int, pat: Sequence[int], current_min: int) -> bool:
101102
# Is MSB target digit to replace or not?
102103
if pat[-1] == 1:
103104
return assemble_num(i, pat, 1) < current_min
@@ -106,7 +107,7 @@ def is_probable(i: int, pat: list[int], current_min: int) -> bool:
106107

107108

108109
# Check all cases
109-
def find_prime(i: int, pat: list[int]) -> int | None:
110+
def find_prime(i: int, pat: Sequence[int]) -> int | None:
110111
# If MSB is target digit to replace, '0' is not applicable.
111112
start = 0
112113
if pat[-1] == 1:

python/euler/bin/p0054.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
# AH JH TH QH KH -> [9, 14, 13, 12, 11, 10]) - Royal Flush [9; 14; 13; 12; 11; 10]
3131

3232
from collections import Counter
33-
from collections.abc import Iterator
33+
from collections.abc import Iterable, Iterator, Sequence
3434
from typing import IO
3535

3636

37-
def get_handrank(hand: list[str]) -> list[int]:
37+
def get_handrank(hand: Iterable[str]) -> list[int]:
3838
def rank_to_num(s: str) -> int:
3939
tbl = {
4040
'2': 2,
@@ -53,10 +53,10 @@ def rank_to_num(s: str) -> int:
5353
}
5454
return tbl[s]
5555

56-
def is_straight(lst: list[int]) -> bool:
56+
def is_straight(lst: Sequence[int]) -> bool:
5757
return lst == list(range(lst[0], lst[0] - 5, -1))
5858

59-
def get_detail(h_info: list[tuple[int, int]]) -> list[int]:
59+
def get_detail(h_info: Iterable[tuple[int, int]]) -> list[int]:
6060
detail, _ = zip(*h_info, strict=True)
6161
return list(detail)
6262

@@ -121,7 +121,7 @@ def parse_data(fh: IO) -> Iterator[list[list[str]]]:
121121
[line.split(' ') for line in fh.read().splitlines()],
122122
)
123123

124-
def determine(lst: list[list[str]]) -> int:
124+
def determine(lst: Sequence[Iterable[str]]) -> int:
125125
p1, p2 = get_handrank(lst[0]), get_handrank(lst[1])
126126
if p1 > p2:
127127
return 1
@@ -130,9 +130,9 @@ def determine(lst: list[list[str]]) -> int:
130130
else:
131131
return 0
132132

133-
hands_lst = parse_data(fh)
133+
hands_it = parse_data(fh)
134134
p1 = p2 = draw = 0
135-
for hands in hands_lst:
135+
for hands in hands_it:
136136
match determine(hands):
137137
case 1:
138138
p1 += 1

0 commit comments

Comments
 (0)