-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfino.py
138 lines (123 loc) · 3.57 KB
/
fino.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
#!/usr/bin/env python
"""
Output the Finnish word for a given integer.
"""
from __future__ import annotations
import argparse
import bisect
LESS_THAN_TEN = {
0: "nolla",
1: "yksi",
2: "kaksi",
3: "kolme",
4: "neljä",
5: "viisi",
6: "kuusi",
7: "seitsemän",
8: "kahdeksan",
9: "yhdeksän",
}
# https://fi.wikipedia.org/wiki/Suurten_lukujen_nimet
SINGULAR_TENS = {
10: "kymmenen",
100: "sata",
1000: "tuhat",
10**6: "miljoona",
10**9: "miljardi",
10**12: "biljoona",
10**18: "triljoona",
10**24: "kvadriljoona",
10**30: "kvintiljoona",
10**36: "sekstiljoona",
10**42: "septiljoona",
10**48: "oktiljoona",
10**54: "noviljoona",
10**60: "dekiljoona",
10**66: "undekiljoona",
10**72: "duodekiljoona",
10**78: "tredekiljoona",
10**84: "kvattuordekiljoona",
10**90: "kvindekiljoona",
10**96: "sedekiljoona",
10**100: "googol",
10**102: "septendekiljoona",
10**108: "duodevigintiljoona",
10**114: "undevigintiljoona",
10**120: "vigintiljoona",
10**126: "unvigintiljoona",
10**180: "trigintiljoona",
10**600: "sentiljoona",
}
# Just the few which don't have a simple -a partitive suffix
PLURAL_TENS = {10: "kymmentä", 1000: "tuhatta", 10**100: "googolia"}
LIST_OF_TENS = sorted(SINGULAR_TENS.keys())
def print_it(text: str) -> None:
"""Windows cmd.exe cannot do Unicode so encode first"""
print(text.encode("utf-8"))
def wordify(number: int, tens: int) -> str:
if number == tens:
return SINGULAR_TENS[tens]
d, m = divmod(number, tens)
if d > 1:
if tens in PLURAL_TENS:
plural = PLURAL_TENS[tens]
else:
plural = SINGULAR_TENS[tens] + "a"
word = to_finnish(d) + plural
else:
word = SINGULAR_TENS[tens]
if m > 0:
word += to_finnish(m)
return word
def find_tens_range(number: int) -> int:
"""
Find where a number comes in the list of tens.
Return the ten before (or equal to it), or None if out of range.
"""
n = bisect.bisect_left(LIST_OF_TENS, number)
if LIST_OF_TENS[n : n + 1] == [number]:
return LIST_OF_TENS[n]
else:
return LIST_OF_TENS[n - 1]
def to_finnish(number: int) -> str:
"""
Given an integer, return the Finnish word for that number.
"""
if isinstance(number, float):
return "en tiedä"
if number < 0:
return "miinus " + to_finnish(abs(number))
elif 0 <= number < 10:
return LESS_THAN_TEN[number]
elif 11 <= number <= 19:
return LESS_THAN_TEN[number % 10] + "toista"
elif number == 10 or 20 <= number <= 99:
return wordify(number, 10)
else:
ten = find_tens_range(number)
return wordify(number, ten)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Output the Finnish word for a given integer.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("number", type=int, help="An input integer")
parser.add_argument(
"end",
nargs="?", # type=int,
help="An optional end integer. Prints all numbers from the first one "
"up to this one. Use 'max' to keep going.",
)
args = parser.parse_args()
if not args.end:
print(to_finnish(args.number))
else:
if args.end == "max":
end = sorted(SINGULAR_TENS.keys())[-1] + 1
else:
end = int(args.end)
i = args.number
while i < end + 1:
print_it(str(i) + " " + to_finnish(i))
i += 1
# End of file