[Tutor] Projects (fwd)
Ricardo Aráoz
ricaraoz at gmail.com
Thu Jan 24 03:20:08 CET 2008
Tiger12506 wrote:
>> up to a thousand (not tested)
>>
>> words = {0:'zero', 1:'one', 2:'two', 3:'three', ... , 10:'ten',
>> 11:'eleven', 12:'twelve', ..., 19:'nineteen',
>> 20:'twenty', ...., 90:'ninety', 100:'one hundred' }
>> def digitToString(n) :
>> try :
>> retStr = words[n]
>> except KeyError :
>> if n > 100 :
>> retStr = (digitToString(n // 100)
>> + ' hundred and '
>> + digitToString(n % 100))
>> else :
>> retStr = (digitToString(n - (n % 10))
>> + ' '
>> + digitToString(n % 10))
>>
>> return retStr
>
> This could be written much more efficiently. It can be done with only these
> lists~
> ones =
> ['zero','one','two','three','four','five','six','seven','eight','nine']
> teens =
> ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
> tens =
> ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']
> hundstr = 'hundred'
> thousand = 'thousand'
>
Isn't dictionary access faster than list access? Why are three lists
'much more efficient'?
More information about the Tutor
mailing list