-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
155 lines (140 loc) · 5.06 KB
/
converter.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
"""
Demo program for how black, flake8, and mypy can help you
dev operable Python!
"""
two_digit_words: list[str] = ["ten", "eleven", "twelve"]
hundred: str = "hundred"
large_sum_words: list[str] = [
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
"sextillion",
"septillion",
"octillion",
"nonillion",
]
one_digit_words: dict[str, list[str]] = {
"0": ["zero"],
"1": ["one"],
"2": ["two", "twen"],
"3": ["three", "thir"],
"4": ["four", "for"],
"5": ["five", "fif"],
"6": ["six"],
"7": ["seven"],
"8": ["eight"],
"9": ["nine"],
}
def convert(n: str) -> str:
"""convert() takes an integer and returns number written out in words"""
word = []
# Zero is a special case
if n == "0":
return "Zero"
if n.startswith("-"):
word.append("(negative)")
n = n[1:]
if len(n) % 3 != 0 and len(n) > 3:
n = n.zfill(3 * (((len(n) - 1) // 3) + 1))
sum_list = [n[i : i + 3] for i in range(0, len(n), 3)]
skip = False
for i, num in enumerate(sum_list):
if num != "000":
skip = False
for _ in range(len(num)):
num = num.lstrip("0")
if len(num) == 1:
if (
(
len(sum_list) > 1
or (len(sum_list) == 1 and len(sum_list[0]) == 3)
)
and i == len(sum_list) - 1
and (word[-1] in large_sum_words or hundred in word[-1])
):
word.append("and")
word.append(one_digit_words[num][0])
num = num[1:]
break
if len(num) == 2:
if num[0] != "0":
if (
len(sum_list) > 1
or (len(sum_list) == 1 and len(sum_list[0]) == 3)
) and i == len(sum_list) - 1:
word.append("and")
if num.startswith("1"):
if int(num[1]) in range(3):
word.append(two_digit_words[int(num[1])])
else:
number = one_digit_words[num[1]][
1 if int(num[1]) in range(3, 6, 2) else 0
]
word.append(
number + ("teen" if not number[-1] == "t" else "een")
)
else:
word.append(
one_digit_words[num[0]][
1 if int(num[0]) in range(2, 6) else 0
]
+ ("ty " if num[0] != "8" else "y ")
+ (one_digit_words[num[1]][0] if num[1] != "0" else "")
)
break
else:
num = num[1:]
continue
if len(num) == 3:
if num[0] != "0":
word.append(one_digit_words[num[0]][0] + " " + hundred)
if num[1:] == "00":
break
num = num[1:]
if len(sum_list[i:]) > 1 and not skip:
word.append(large_sum_words[len(sum_list[i:]) - 2])
skip = True
"""
This section shows the issues with unexpected type changes.
The original code converts list[str] to str without notice.
Mypy complains about this, so you can fix it my declaring
a new variable of type str.
"""
# # # This block is the original code
# # Uncomment for type debugging for "word"
# # print(word, "is of type:", type(word))
# # print("***")
# word = " ".join(map(str.strip, word))
# # Uncomment for type debugging for "word"
# print(word, "is of type:", type(word))
# return (
# word[0].lstrip().upper() + word[1:].rstrip().lower()
# if "negative" not in word
# else word[:11].lstrip() + word[11].upper() + word[12:].rstrip().lower()
# )
# This block is the fix for mypy
print(word, "is of type:", type(word))
result_word = " ".join(map(str.strip, word))
# Uncomment for type debugging for "word"
# print(word, "is of type:", type(word))
# print(result_word, "is of type:", type(result_word))
return (
result_word[0].lstrip().upper() + result_word[1:].rstrip().lower()
if "negative" not in result_word
else result_word[:11].lstrip()
+ result_word[11].upper()
+ result_word[12:].rstrip().lower()
)
if __name__ == "__main__":
while True:
try:
n = input("Enter any number to convert it into words or 'exit' to stop: ")
if n == "exit":
break
int(n)
print(n, "-->", convert(n))
except ValueError:
print("Error: Invalid Number!")