Skip to content

Instantly share code, notes, and snippets.

@kurthaeusler
Created August 18, 2011 21:07
Show Gist options
  • Select an option

  • Save kurthaeusler/1155207 to your computer and use it in GitHub Desktop.

Select an option

Save kurthaeusler/1155207 to your computer and use it in GitHub Desktop.
An exercise I came across on reddit.
import random
def convert(number):
return {
0 : "Zero.wav",
1 : "One.wav",
2 : "Two.wav",
3 : "Three.wav",
4 : "Four.wav",
5 : "Five.wav",
6 : "Six.wav",
7 : "Seven.wav",
8 : "Eight.wav",
9 : "Nine.wav",
10 : "Ten.wav",
11 : "Eleven.wav",
12 : "Twelve.wav",
13 : "Thirteen.wav",
14 : "Fourteen.wav",
15 : "Fifteen.wav",
16 : "Sixteen.wav",
17 : "Seventeen.wav",
18 : "Eighteen.wav",
19 : "Nineteen.wav",
20 : "Twenty.wav",
30 : "Thirty.wav",
40 : "Fourty.wav",
50 : "Fifty.wav",
60 : "Sixty.wav",
70 : "Seventy.wav",
80 : "Eighty.wav",
90 : "Ninety.wav"
}[number]
def convert1to9999(number):
output = ""
output = output + convert1to999(number, False)
number = number % 100
output = output + " And.wav " + convert1to99(number)
output = output + " Thousand.wav "
return output
def convert1to999(number, hundreds):
output = ""
if hundreds == False and number > 99:
output = output + convert1to999(number / 100, True)
return output
output = output + convert1to99(number)
if hundreds == True:
output = output + " Hundred.wav"
return output
def convert1to99(number):
output = ""
if number > 20:
tens = number / 10
ones = number % 10
output = output + convert(tens * 10)
if ones > 0:
output = output + " " + convert(ones)
else:
output = output + convert(number)
return output
def say(cents):
output = ""
if cents > 99999:
thousands = cents / 100000
output = output + convert1to9999(thousands)
cents = cents - (thousands * 100000)
if cents / 100 == 0:
output = output + "Dollars.wav"
if cents == 0:
return output
if cents > 9999:
hundreds = cents / 10000
output = output + convert1to999(hundreds, True)
cents = cents - (hundreds * 10000)
if cents / 100 == 0:
output = output + " Dollars.wav"
else:
output = output + " And.wav "
if cents == 0:
return output
if cents > 99:
dollars = cents / 100
output = output + convert1to99(dollars)
if dollars == 1:
output = output + " Dollar.wav"
else:
output = output + " Dollars.wav"
cents = cents - (dollars * 100)
if cents == 0:
return output
if len(output) > 0 and cents > 0:
output = output + " And.wav "
output = output + convert1to99(cents)
output = output + " Cents.wav"
return output
for i in range(10000):
cents = random.randint(0,99999999)
print cents
print say(cents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment