Created
January 15, 2014 02:41
-
-
Save ishahid/8429867 to your computer and use it in GitHub Desktop.
Convert numbers to their string representation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
units = { | |
0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine' | |
} | |
tens = { | |
10: 'Ten', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', | |
17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty', | |
60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety' | |
} | |
groups = { | |
0: '', 1: 'Thousand', 2: 'Million', 3: 'Billion', 4: 'Trillion', 5: 'Quadrillion', 6: 'Quintrillion', | |
7: 'Sextillion', 8: 'Septillion', 9: 'Octillion', 10: 'Nonillion', 11: 'Decillion' | |
} | |
def convertDigit(d): | |
return units[d] | |
def convertTwoDigits(d1, d2): | |
if d2 == 0: | |
return tens[d1 * 10] | |
elif d1 == 1: | |
return tens[d1 * 10 + d2] | |
else: | |
return tens[d1 * 10] + ' ' + convertDigit(d2) | |
def convertThreeDigits(d1, d2, d3): | |
if d1 == 0 and d2 == 0 and d3 == 0: | |
return '' | |
output = '' | |
if d1 != 0: | |
output = convertDigit(d1) + ' Hundred' | |
if d2 != 0: | |
output = output + ' ' + convertTwoDigits(d2, d3) | |
elif d3 != 0: | |
output = output + ' ' + convertDigit(d3) | |
return output | |
def convertGroup(index): | |
return groups[index] | |
def chunks(s, n): | |
for start in range(0, len(s), n): | |
yield s[start:start + n] | |
def num2str(number): | |
output = '' | |
num = str(number) | |
if num[0] == '-': | |
output = 'Negative ' | |
num = num[1:] | |
elif num[0] == '+': | |
num = num[1:] | |
if num[0] == '0': | |
output = output + 'Zero' | |
else: | |
num = num.zfill(36) | |
groups = chunks(num, 3) | |
z = 0 | |
for group in groups: | |
temp = convertThreeDigits(int(group[0]), int(group[1]), int(group[2])) | |
if temp != '': | |
output = output + temp + ' ' + convertGroup(11 - z) + ' ' | |
z = z + 1 | |
output = output.strip() | |
output = re.sub(r'[ ]+', r' ', output) | |
return output | |
if __name__ == '__main__': | |
assert 'One' == num2str(1) | |
assert 'Twelve' == num2str(12) | |
assert 'Seventy Two' == num2str(72) | |
assert 'Three Hundred' == num2str(300) | |
assert 'One Thousand Two Hundred' == num2str(1200) | |
assert 'Twelve Thousand Three Hundred Four' == num2str(12304) | |
assert 'Six Million' == num2str(6000000) | |
assert 'Six Million Four Hundred Thousand Five' == num2str(6400005) | |
assert 'One Hundred Twenty Three Billion Four Hundred Fifty Six Million Seven Hundred Eighty Nine Thousand Twelve' == num2str(123456789012) | |
assert 'Four Decillion' == num2str(4000000000000000000000000000000000) | |
print 'All tests passed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment