-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_to_words.py
More file actions
164 lines (143 loc) · 7.09 KB
/
number_to_words.py
File metadata and controls
164 lines (143 loc) · 7.09 KB
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
156
157
158
159
160
161
162
163
164
from well_written import well_written
def translate_1(chaine):
valeurs = ['zero','one','two','three','four','five','six','seven','eight','nine']
indexe = int(chaine)
resultat = valeurs[indexe]
return resultat
def translate_2(chaine):
if chaine[0] == "0":
return translate_1(chaine[1])
elif chaine[0] == "1":
valeurs = ['ten','eleven' ,'twelve' ,'thirteen' ,'fourteen' ,'fifteen' ,'sixteen' ,'seventeen' ,'eighteen' ,'nineteen' ,'twenty']
indexe = int(chaine[1])
return valeurs[indexe]
elif chaine[1] == "0":
valeurs = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
indexe = int(chaine[0])-2
return valeurs[indexe]
else:
valeurs = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
unite = chaine[1]
indice_dizaine = int(chaine[0])-2
dizaine = valeurs[indice_dizaine]
unite_traduit = translate_1(unite)
resultat = dizaine + "-" + unite_traduit
return resultat
def translate_3(chaine):
if chaine[0] == "0":
return translate_2(chaine[1:])
elif chaine[1] == "0" and chaine[2]=="0":
valeurs = ['one hundred','two hundred','three hundred','four hundred','five hundred','six hundred','seven hundred','eight hundred','nine hundred']
indexe = int(chaine[0])-1
return valeurs[indexe]
else:
valeurs = ['one hundred','two hundred','three hundred','four hundred','five hundred','six hundred','seven hundred','eight hundred','nine hundred']
reste = chaine[0]
indice_premier = int(reste)-1
premier = valeurs[indice_premier]
reste_traduit = translate_2(chaine[1:])
resultat = premier + " and " + reste_traduit
return resultat
def translate_4(chaine):
if chaine[0] == "0":
return translate_3(chaine[1:])
elif chaine[1] == "0" and chaine[2]=="0" and chaine[3]=="0":
valeurs = ['one thousand','two thousand','three thousand','four thousand','five thousand','six thousand','seven thousand','eight thousand','nine thousand']
indexe = int(chaine[0])-1
return valeurs[indexe]
else:
valeurs = ['one thousand','two thousand','three thousand','four thousand','five thousand','six thousand','seven thousand','eight thousand','nine thousand']
reste = chaine[0]
indice_premier = int(reste)-1
premier = valeurs[indice_premier]
reste_traduit = translate_3(chaine[1:])
resultat = premier + " " + reste_traduit
return resultat
def translate_5(chaine):
if chaine[0] == "0":
return translate_4(chaine[1:])
elif chaine[1] == "0" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0":
valeurs = ['ten thousand','twenty thousand','thrity thousand','forty thousand','fifty thousand','sixty thousand','seventy thousand','eighty thousand','ninety thousand']
indexe = int(chaine[0])-1
return valeurs[indexe]
elif chaine[0] == "1" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0":
valeurs = ['eleven thousand','twelve thousand','thirteen thousand','fourteen thousand','fifteen thousand','sixteen thousand','seventeen thousand','eighteen thousand','nineteen thousand']
indexe = int(chaine[1])-1
return valeurs[indexe]
elif chaine[0] == "1":
valeurs = ['eleven thousand','twelve thousand','thirteen thousand','fourteen thousand','fifteen thousand','sixteen thousand','seventeen thousand','eighteen thousand','nineteen thousand']
indice_premier = int(chaine[1])-1
premier = valeurs[indice_premier]
reste_traduit = translate_3(chaine[2:])
resultat = premier +" "+ reste_traduit
return resultat
else:
valeurs = ["twenty thousand", "thirty thousand", "forty thousand", "fifty thousand", "sixty thousand", "seventy thousand", "eighty thousand", "ninety"]
if chaine[1] == "0" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0":
indexe = int(chaine[0])-2
return valeurs[indexe]
else:
indice_premier = int(chaine[0])-2
premier = valeurs[indice_premier]
reste_traduit = translate_4(chaine[1:])
resultat = premier +" "+ reste_traduit
return resultat
def translate_5_2(chaine):
if chaine[0] == "0":
return translate_4(chaine[1:])
elif chaine[1] == "0" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0":
valeurs = ['ten ','twenty ','thrity ','forty ','fifty ','sixty ','seventy ','eighty ','ninety ']
indexe = int(chaine[0])-1
return valeurs[indexe]
elif chaine[0] == "1" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0":
valeurs = ['eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen ']
indexe = int(chaine[1])-1
return valeurs[indexe]
elif chaine[0] == "1":
valeurs = ['eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen ']
indice_premier = int(chaine[1])-1
premier = valeurs[indice_premier]
reste_traduit = translate_3(chaine[2:])
resultat = premier +" "+ reste_traduit
return resultat
else:
valeurs = ["twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety"]
if chaine[1] == "0" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0":
indexe = int(chaine[0])-2
return valeurs[indexe]
else:
indice_premier = int(chaine[0])-2
premier = valeurs[indice_premier]
reste_traduit = translate_4(chaine[1:])
resultat = premier +" "+ reste_traduit
return resultat
def translate_6(chaine):
if chaine[0] == "0":
return translate_5(chaine[1:])
else:
valeurs = ['one hundred ','two hundred ','three hundred ','four hundred ','five hundred ','six hundred ','seven hundred ','eigth hundred ','nine hundred ']
indice_premier = int(chaine[0])-1
premier = valeurs[indice_premier]
reste_traduit = translate_5_2(chaine[1:])
resultat = premier +" "+ reste_traduit
return resultat
def translate_7(chaine):
if chaine[0] == "0":
return translate_6(chaine[1:])
elif chaine[1] == "0" and chaine[2]=="0" and chaine[3]=="0" and chaine[4]=="0" and chaine[5]=="0" and chaine[6]=="0":
valeurs = ['one ','two ','three ','four ','five ','six ','seven ','eigth ','nine ']
indexe = int(chaine[0])-1
if indexe == 0:
return valeurs[indexe] + 'million'
else:
return valeurs[indexe] + 'millions'
else:
valeurs = ['one','two','three','four','five','six','seven','eigth','nine']
indice_premier = int(chaine[0])-1
premier = valeurs[indice_premier]
reste_traduit = translate_6(chaine[1:])
if indice_premier == 0:
resultat = premier +" million "+ reste_traduit
else:
resultat = premier +" millions "+ reste_traduit
return resultat