2014-03-12 + HW

posted Mar 12, 2014, 5:37 AM by Samuel Konstantinovich   [ updated Mar 12, 2014, 5:51 AM ]
HOMEWORK:  make your numToWords(x)   work for numbers from -999,999,999 to 999,999,999 inclusive.

In class you now have time to make your numToWords() work on very large numbers!

Presumably you have code that converts 1-999 into the words.

Consider the function numToWords(123456789) which can be broken down:

numToWords999(123)+' million '+numToWords999(456)+' thousand '+numToWords999(789)


You can do this recursively by keeping track of which group of 3 digits you are processing...

numToWords(123456789) would call a helper function: numToWords(123456789,0)


numToWordsBig(123456789,0)  #handle the ones with your 999 function.
numToWordsBig(123456,1) numToWords999(789)    

The next call:
numToWordsBig(123456,1) numToWords999(789)    #handle the thousands
numToWordsBig(123,2) + numToWords999(456)+" thousand "+ numToWords999(789) 

The next call
numToWordsBig(123,2) numToWords999(456)+" thousand "+ numToWords999(789)  
#handle the millions
numToWords999(123)+ "million "numToWords999(456)+" thousand "+ numToWords999(789)

Right now you need to have an if-statement to handle each new column of 3 values, one for the thousands, one for the millions. If you wanted to expand it, you could continue to very large values easily.     

Your goal:  make your numToWords(x)   work for numbers from -999,999,999 to 999,999,999 inclusive.
You have to make your numToWords() call numToWordsBig() which in turn uses numToWords999.

This is homework, due tomorrow.


Comments