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'? _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor