Skip to content

Commit 3620714

Browse files
committed
Add string to number converter
1 parent a71618f commit 3620714

2 files changed

Lines changed: 193 additions & 1 deletion

File tree

strings/min_cost_string_conversion.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
140140
elif op[0] == "R":
141141
string[i] = op[2]
142142

143-
file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))) # noqa: UP031
143+
file.write(
144+
"%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))
145+
) # noqa: UP031
144146
file.write("\t\t" + "".join(string))
145147
file.write("\r\n")
146148

strings/string_to_num.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
"""Converts a given string to integer and float
2+
This works with only Indian system of wording
3+
4+
* Indian system uses crore, lakh, thousand and not million and billions
5+
6+
For the part after the decimal example ( .159 ):
7+
* Digit by digit ( .159 ) -> point one five nine is allowed
8+
* Anything else will throw an error
9+
10+
>>> to_int("Five")
11+
5
12+
>>> to_float("Five")
13+
5.0
14+
15+
>>> to_int("One thousand five hundred and two")
16+
1502
17+
>>> to_float("One thousand five hundred and two")
18+
1502.0
19+
20+
>>> to_int(
21+
... "Ninety nine crore three lakh seventy two thousand and six point one five nine"
22+
... )
23+
990372006
24+
25+
>>> to_float(
26+
... "Ninety nine crore three lakh seventy two thousand and six point one five nine"
27+
... )
28+
990372006.159
29+
30+
wikipedia explanation - https://en.wikipedia.org/wiki/Numeral_(linguistics)
31+
"""
32+
33+
34+
def to_int(word: str) -> int:
35+
if len(word.strip()) > 0:
36+
units = {
37+
"zero": 0,
38+
"one": 1,
39+
"two": 2,
40+
"three": 3,
41+
"four": 4,
42+
"five": 5,
43+
"six": 6,
44+
"seven": 7,
45+
"eight": 8,
46+
"nine": 9,
47+
"eleven": 11,
48+
"twelve": 12,
49+
"thirteen": 13,
50+
"fourteen": 14,
51+
"fifteen": 15,
52+
"sixteen": 16,
53+
"seventeen": 17,
54+
"eighteen": 18,
55+
"nineteen": 19,
56+
}
57+
58+
tens = {
59+
"ten": 10,
60+
"twenty": 20,
61+
"thirty": 30,
62+
"forty": 40,
63+
"fifty": 50,
64+
"sixty": 60,
65+
"seventy": 70,
66+
"eighty": 80,
67+
"ninety": 90,
68+
}
69+
70+
multipliers = {
71+
"hundred": 100,
72+
"thousand": 1_000,
73+
"lakh": 1_00_000,
74+
"crore": 1_00_00_000,
75+
}
76+
77+
if "point" in word:
78+
word_lst = word.split("point")
79+
word = "".join(word_lst[:-1])
80+
81+
words = (
82+
word.strip()
83+
.replace(" and", "")
84+
.replace("-", "")
85+
.replace("_", "")
86+
.lower()
87+
.split()
88+
)
89+
90+
number = 0
91+
temp = 0
92+
93+
for index, word in enumerate(words):
94+
if index == 0:
95+
if word in units:
96+
temp += units[word]
97+
elif word in tens:
98+
temp += tens[word]
99+
else:
100+
temp += multipliers[word]
101+
elif index == (len(words) - 1):
102+
if word in units:
103+
temp += units[word]
104+
number += temp
105+
elif word in tens:
106+
temp += tens[word]
107+
number += temp
108+
else:
109+
temp *= multipliers[word]
110+
number += temp
111+
elif word in units:
112+
temp += units[word]
113+
elif word in tens:
114+
temp += tens[word]
115+
elif word in multipliers:
116+
temp *= multipliers[word]
117+
number += temp
118+
temp = 0
119+
120+
if len(words) > 1:
121+
return number
122+
else:
123+
return temp
124+
else:
125+
raise ValueError("Empty input is not a valid number in words")
126+
127+
128+
def to_float(word: str) -> float:
129+
units = {
130+
"zero": 0,
131+
"one": 1,
132+
"two": 2,
133+
"three": 3,
134+
"four": 4,
135+
"five": 5,
136+
"six": 6,
137+
"seven": 7,
138+
"eight": 8,
139+
"nine": 9,
140+
"eleven": 11,
141+
"twelve": 12,
142+
"thirteen": 13,
143+
"fourteen": 14,
144+
"fifteen": 15,
145+
"sixteen": 16,
146+
"seventeen": 17,
147+
"eighteen": 18,
148+
"nineteen": 19,
149+
}
150+
151+
all_words = (
152+
word.strip()
153+
.replace(" and", "")
154+
.replace("-", "")
155+
.replace("_", "")
156+
.lower()
157+
.split("point")
158+
)
159+
if len(all_words) > 1:
160+
word = all_words[0]
161+
after_point = all_words[1].split()
162+
163+
integer_part = to_int(word)
164+
165+
decimal_part = ""
166+
for num in after_point:
167+
if num in units:
168+
decimal_part += str(units[num])
169+
170+
str_float = str(integer_part) + str(decimal_part)
171+
divider = "1" + ("0" * len(after_point))
172+
return int(str_float) / int(divider)
173+
174+
else:
175+
return float(to_int(word))
176+
177+
178+
if __name__ == "__main__":
179+
import doctest
180+
181+
doctest.testmod()
182+
# while True:
183+
# word = input("Enter a number in words (q to quit) :- ").lower().strip()
184+
# if word == "q":
185+
# break
186+
# else:
187+
# integer = to_int(word)
188+
# print(f"\nThe number in {type(integer)} --> {integer} ")
189+
# floater = to_float(word)
190+
# print(f"\nThe number in {type(floater)} --> {floater} ")

0 commit comments

Comments
 (0)