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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
| # -*- coding: cp1252 -*-
from time import clock
t1 = clock()
for xx in xrange(100):
ADJCTV_NUMBERS = { '0': "zéro" ,
'1': "un" , '2': "deux" , '3': "trois" ,
'01': "un" , '02': "deux" , '03': "trois" ,
'4': "quatre" , '5': "cinq" , '6': "six" ,
'04': "quatre" , '05': "cinq" , '06': "six" ,
'7': "sept" , '8': "huit" , '9': "neuf" ,
'07': "sept" , '08': "huit" , '09': "neuf" ,
'10': "dix" ,
'11': "onze" , '12': "douze" , '13': "treize" ,
'14': "quatorze" , '15': "quinze" , '16': "seize" ,
'17': "dix-sept" , '18': "dix-huit" , '19': "dix-neuf" ,
'20': "vingt" , '21': 'vingt et un' ,
'30': "trente" , '31': 'trente et un' ,
'40': "quarante" , '41': 'quarante et un' ,
'50': "cinquante" , '51': 'cinquante et un' ,
'60': "soixante" , '61': 'soixante et un' ,
'70': "soixante-dix" , '71': 'soixante et onze' ,
'80': "quatre-vingts" ,
'90': "quatre-vingt-dix" ,
'100': "cent" , '200': "deux cents", '300': "trois cents",
'400': "quatre cents", '500': "cinq cents", '600': "six cents" ,
'700': "sept cents" , '800': "huit cents", '900': "neuf cents" }
# We will use special prefix for numbers 'x2' to 'x9' with x = 2,3,4,5,6,7
# and for numbers '81' to '89' and '91' to '99'.
TEN_PREFIXES = { '2': "vingt-" , '3': "trente-" ,
'4': "quarante-" , '5': "cinquante-" , '6': "soixante-" ,
'7': "soixante-" , '8': "quatre-vingt-" , '9': "quatre-vingt-" }
# The following dictionary will be used to name
# a number by finding its pieces of three digits.
# Système de noms utilisé: échelle longue.
TEN_POWERS = { 6: 'MILLION' , 9: 'MILLIARD' ,
12: 'BILLION' , 15: 'BILLIARD' ,
18: 'TRILLION' , 21: 'TRILLIARD' ,
24: 'QUADRILLION' , 27: 'QUADRILLIARD' ,
30: 'QUINTILLION' , 33: 'QUINTILLIARD' ,
36: 'SEXTILLION' , 39: 'SEXTILLIARD' }
aa = '1000'+len(TEN_POWERS)*'000'
TEN_POWERS_NUMBERS = tuple( aa[0:1+p] for p in TEN_POWERS )
# We find the biggest number of digits allowed.
BIGGEST_DIGITS_NUMBER = max(TEN_POWERS) + 3
t2 = clock()
def printer(number, checkValidity = False):
if checkValidity:
try:
number = str(int(number)) # We work with a string and we clean unusefull zeros...
except:
raise ValueError('number = "' + str(number) + '" must be an integer')
# If we have a base adjective number.
if number in ADJCTV_NUMBERS:
return ADJCTV_NUMBERS[number]
# Now, the number is necessarily of length > 1 because all the 1-digit numbers
# are in the ADJCTV_NUMBERS dictionnary.
# If we have a 2 digits number, first digit can't be '0', even with checkValidity==False
# because numbers '01','02','03','04','05','06','07','08','09' have been already treated.
# The numbers '11','12','13','14','15','16','17','18','19','20','21' and '30','31','40',
# '41','50','51','60','61','70','71' and '80','90' have also been already treated.
# We have to take care of things like 76 and 94.
elif len(number) == 2:
if number[0] in '79':
return TEN_PREFIXES[number[0]] + ADJCTV_NUMBERS['1' + number[1]]
else: # that is to say number[0] in ('2','3','4','5','6','8')
return TEN_PREFIXES[number[0]] + ADJCTV_NUMBERS[number[1]]
# We have a number between '101' and '999'.
# Hundreds '100','200','300','400','500','600','700','800','900' have been already treated.
# So number[1:] can't be '00' and printer(number[1:]) always exists.
# Numbers of length 3 can't be of type '0xx': if checkValidity==True, the heading '0' has
# been removed by str(int(number)); if number have been called upon with printer(number)
# with checkValidity==False by default, the number have been previously treated with
# lstrip('0')(see section len(number) <= BIGGEST_DIGITS_NUMBER)
elif len(number) == 3:
if number[0]!='1': # for numbers '201' to '299', '301' to '399', ...., '901' to '999'.
return ADJCTV_NUMBERS[number[0]] + ' cent ' + printer(number[1:])
else: # for numbers '101' to '199'
return 'cent ' + printer(number[1:])
# TEN_POWERS_NUMBERS = [aa[0:7+p] for p in xrange(0,len(TEN_POWERS)*3,3)]
elif number in TEN_POWERS_NUMBERS:
return 'un ' + TEN_POWERS[len(number)-1]
# We have to split the number in pieces of three digits.
elif len(number) <= BIGGEST_DIGITS_NUMBER:
li = [ printer(number[-q-9:-q-6].lstrip('0'))\
+ ' ' + TEN_POWERS[q+6] + (number[-q-9:-q-6] not in ('001','1'))*'S'
for q in xrange(0,len(number)-6,3) if number[-q-9:-q-6]!='000' ]
li.reverse()
if number[-6:-3] in ('1','001'):
li.append('MILLE')
elif int(number[-6:-3]):
li.append( (printer(number[-6:-3].lstrip('0')) + ' MILLE').replace('ts MILLE','t MILLE') )
if int(number[-3:]):
li.append( printer(number[-3:].lstrip('0')) )
return ' '.join(li)
else:
raise ValueError('number = "' + str(number) + '" is too big.')
###############
###############
## ##
## FOR TESTS ##
## ##
###############
###############
if __name__ == '__main__':
import random
# 0 = randomTest
# 1 = calculs de temps sur 100 nombres et 100 repetitions
# 2 resultats sur une selection de nombres
# 4 temps en fonction de la longueur du nombre traduit
choix = 1
if choix==0: # randomTest:
nbOfTest_in_each = 3
g = ( random.randint(10**p,10**(p+1)-1) for p in xrange(0,BIGGEST_DIGITS_NUMBER)
for i in xrange(nbOfTest_in_each) )
print '\n\n'.join( '* '+str(oneNumber)+'\t' + printer(oneNumber,checkValidity = True)
for oneNumber in g ) + '\n\n'
if choix==1:
g = (1580 ,4580 ,8080 ,7856 ,6587 ,5234 ,
29400 ,56890 ,45378 ,34565 ,87275 ,87266 ,
345627 ,676448 ,987635 ,897733 ,334559 ,939847 ,
5053054 ,9000658 ,9800727 ,3456486 ,1001980 ,1003801 ,
79003801 ,87080004 ,40000056 ,56700080 ,58008050 ,24555255 ,
700100000 ,123456081 ,340018480 ,380407080 ,345346366 ,236346366 ,
845534555 ,100033000 ,800000400 ,880905004 ,453223525 ,364574457 ,
3453453355,4353455555,9046571455,4773377777,8223467734,8564345523,
77764565445,
999999999999,
3778276346674,
28359872639829,
772387682736876,
1287815288647698,
16638273948723424,
726472365472545823,
8768176486787162869,
98719694691214212444,
989897169649819294981,
8728734827687142772933,
89894875982837582698299,
909697465868375676252342,
9978768762857628734824877,
87643768758254656545871877,
873247652833857475847874684,
8772874576354871878769692347,
87347617243716258417268587624,
676628572874916918625752837777,
8768726872864569700409849829848,
88762875876582787037047307982394,
987987129879192687876434798733144,
1098749868787629877917020090398123,
99879746875687268235234234234234666,
666283768476827683476263646709863455,
8787835876121721737128387182361355536,
91797712964162592903747041443454535314,
918961872648716949129891971927971976212,
9198169289798172871298379127937197390812,
98719761238716287619619918928798172987918,
891798129891872390710370170273071093091237,
10**21,10**24,10**27,'1'+30*'0','10'+16*'00',
10000000000,100000000000,
600080327103,500080759800,90000018400,9001000000000,
600000180000000076565543976,700000000180000000043976,
243546780874039872674836252308374664387,5*'10101',8*10**5+2*100**4,
'1000' + '0'*9,'6000' + '0'*13,'78954355'+4*'00'+'83',42*'9')
print 'liste de nombres testee: '+str(len(list(g)))+' nombres'
print 'temps pour 100 executions repetees'
print '\ndefinitions des donnees de base:\n',t2-t1,'secondes'
for yy in xrange(10):
te =clock()
for y in xrange(100):
for oneNumber in g:
x = printer(oneNumber,checkValidity = True)
tf = clock()
print "\ntemps d'execution des 100 repetitions sur les "+str(len(list(g)))+" nombres:"
print (tf-te)
print "temps d'execution moyen par nombre des 100 repetitions:"
print (tf-te)/len(list(g))
if choix==2:
g = (0000000000000000,'0000000',0,
1,2,3,6,8,13,17,20,21,22,26,31,32,40,41,45,51,59,60,61,63,
70,71,73,77,80,81,82,90,91,96,98,
100,106,110,179,180,187,194,297,300,469,508,519,571,780,899,
1000,1580,29400,600530,900068,980077,
1001980,1003801,79003801,87080004,400000567,567000800,580080500,
67900000,67000000,67000001,
7001000000,12345678081,34001678480,380407000480,
1000000,80000000,880905004,
10000000000,100000000000,100000000010,999999999999,
1000000000000,1000000000000000,10000000000000000000,
10**21,10**24,10**27,'1'+30*'0','10'+16*'00',
600080327103,500080759800,90000018400,9001000000000,
600000180000000043976,700000000180000000043976,898000411000180000000043976,
243546780874039872674836252308374664387,5*'10101',8*10**5+2*100**4,
'1000' + '0'*9,'6000' + '0'*13,'78954355'+4*'00'+'83',
485000000000008760004560000000000003456,42*'9')
ecr = '\n\n'.join( '* '+str(oneNumber)+'\t' + printer(oneNumber,checkValidity = True)
for oneNumber in g ) + '\n\n'
print ecr
if choix==3:
print
for numb in (4,44,444,4444,44444,444444,4444444,44444444,444444444,4444444444,
44444444444,444444444444,4444444444444,44444444444444,444444444444444,
4444444444444444,44444444444444444,
444444444444444444,'444444444444444444',
4444444444444444444,'4444444444444444444'):
te =clock()
for y in xrange(100):
x = printer(numb,checkValidity = True)
tf = clock()
print len(str(numb)),tf-te |
Partager