Skip to content

Commit 78d7592

Browse files
committed
Use mypy
1 parent 34ce05a commit 78d7592

File tree

128 files changed

+213
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+213
-151
lines changed

python/GNUmakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ lint:
3232
format:
3333
uvx ruff format --diff .
3434

35+
.PHONY: type-check
36+
type-check:
37+
uvx mypy .
38+
3539
.PHONY: list
3640
list:
3741
@ls euler/bin/p*.py | sed -e 's/.py.*//' -e 's#.*/##' -e 's/^p0*//'

python/euler/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# flake8: noqa
22
from .main import main
3+
4+
__all__ = ['main']

python/euler/bin/p0022.py

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

33
from collections.abc import Iterable
4-
from typing import IO
4+
from typing import TextIO
55

66

77
def total_score(words: Iterable[str]) -> int:
8-
def score(word):
8+
def score(word: str) -> int:
99
return sum(ord(c) - (ord('A') - 1) for c in word)
1010

1111
return sum(idx * score(word) for idx, word in enumerate(words, start=1))
1212

1313

14-
def compute(fh: IO) -> str:
14+
def compute(fh: TextIO) -> str:
1515
keywords = [s.strip('"') for s in fh.read().split(',')]
1616

1717
return str(total_score(sorted(keywords)))

python/euler/bin/p0034.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# according to the order of the input *iterable*. Otherwise, we need to use other comparisons.
5959
def search_from_rhs() -> int:
6060
def to_digit_tpl(n: int) -> tuple[int, ...]:
61-
def aux(n):
61+
def aux(n: int) -> list[int]:
6262
if n >= 10:
6363
return aux(n // 10) + [n % 10]
6464
else:

python/euler/bin/p0040.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# ...
1313
# block #n: n-digits number
1414

15+
from typing import cast
16+
1517

1618
# 0.123456789 | 10111213...979899 | 100101102...997998999 | 100010011002 ...
1719
# ^[d15]=2
@@ -33,7 +35,7 @@ def d(pos: int) -> int:
3335
q, r = (pos - 1) // ndigits, (pos - 1) % ndigits
3436
num = 10 ** (ndigits - 1) + q
3537

36-
return (num // (10 ** (ndigits - r - 1))) % 10
38+
return cast(int, (num // (10 ** (ndigits - r - 1))) % 10)
3739

3840

3941
def compute() -> str:

python/euler/bin/p0042.py

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

33
from collections.abc import Iterable
4-
from typing import IO
4+
from typing import TextIO
55

66
from euler.lib.util import is_triangular
77

@@ -16,7 +16,7 @@ def score(word: str) -> int:
1616
return list(map(score, words))
1717

1818

19-
def compute(fh: IO) -> str:
19+
def compute(fh: TextIO) -> str:
2020
keywords = [s.strip('"') for s in fh.read().split(',')]
2121

2222
return str(len(list(filter(is_triangular, calc_scores(keywords)))))

python/euler/bin/p0054.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
from collections import Counter
3333
from collections.abc import Iterable, Iterator, Sequence
34-
from typing import IO
34+
from typing import TextIO
3535

3636

3737
def get_handrank(hand: Iterable[str]) -> list[int]:
@@ -114,8 +114,8 @@ def get_detail(h_info: Iterable[tuple[int, int]]) -> list[int]:
114114
return handrank
115115

116116

117-
def compute(fh: IO) -> str:
118-
def parse_data(fh: IO) -> Iterator[list[list[str]]]:
117+
def compute(fh: TextIO) -> str:
118+
def parse_data(fh: TextIO) -> Iterator[list[list[str]]]:
119119
return map(
120120
lambda lst: [lst[0:5], lst[5:]],
121121
[line.split(' ') for line in fh.read().splitlines()],

python/euler/bin/p0059.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
from collections.abc import Collection, Iterable
66
from itertools import product
7-
from typing import IO
7+
from typing import TextIO
88

99

1010
def decode(cipher_text: Collection[int], key: tuple[int, ...]) -> list[int]:
@@ -24,7 +24,7 @@ def calc_score(lst: Iterable[int]) -> int:
2424
return cnt
2525

2626

27-
def compute(fh: IO) -> str:
27+
def compute(fh: TextIO) -> str:
2828
cipher_text = list(map(int, fh.read().split(',')))
2929
key_lst = product(range(ord('a'), ord('z') + 1), repeat=3)
3030

python/euler/bin/p0067.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from functools import reduce
55
from itertools import pairwise
66
from operator import add
7-
from typing import IO
7+
from typing import TextIO
88

99

10-
def parse_data(fh: IO) -> list[list[int]]:
10+
def parse_data(fh: TextIO) -> list[list[int]]:
1111
return [list(map(int, line.split(' '))) for line in fh.read().splitlines()]
1212

1313

14-
def compute(fn: Callable[..., int], fh: IO) -> str:
14+
def compute(fn: Callable[..., int], fh: TextIO) -> str:
1515
return str(
1616
reduce(
1717
lambda x, y: list(map(add, map(fn, pairwise(x)), y)),

python/euler/bin/p0070.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from functools import reduce
2020
from math import isqrt
2121
from operator import mul
22+
from typing import cast
2223

2324
from euler.lib.prime import prev_prime, primes
2425
from euler.lib.util import HeapQueue
@@ -27,11 +28,11 @@
2728

2829

2930
def prod(pf_lst: Iterable[tuple[int, int]]) -> int:
30-
return reduce(mul, map(lambda tpl: pow(tpl[0], tpl[1]), pf_lst))
31+
return cast(int, reduce(mul, map(lambda tpl: pow(tpl[0], tpl[1]), pf_lst)))
3132

3233

3334
def phi(pf_lst: Iterable[tuple[int, int]]) -> int:
34-
return reduce(mul, map(lambda tpl: pow(tpl[0], tpl[1] - 1) * (tpl[0] - 1), pf_lst))
35+
return cast(int, reduce(mul, map(lambda tpl: pow(tpl[0], tpl[1] - 1) * (tpl[0] - 1), pf_lst)))
3536

3637

3738
def get_ratio(pf_lst: Iterable[tuple[int, int]]) -> float:

0 commit comments

Comments
 (0)