Created
March 30, 2017 02:38
-
-
Save EdenShapiro/34a386c02d36f9b635f8911d3ae5c5ea to your computer and use it in GitHub Desktop.
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
# Integer -> String | |
# 1 to 999999999 | |
#create global dictionaries of specific "number" words | |
onesDic = {"1": "one ", "2": "two ", "3": "three ", "4": "four ", "5": "five ", "6": "six ", "7": "seven ", "8": "eight ", "9": "nine "} | |
teensDic = {"0": "ten ", "1": "eleven ", "2": "twelve ", "3": "thirteen ", "4":"fourteen ", "5":"fifteen ", "6":"sixteen ", "7":"seventeen ", "8":"eighteen ", "9": "nineteen "} | |
entysDic ={"2":"twenty ", "3": "thirty ", "4": "forty ", "5":"fifty ", "6":"sixty ", "7":"seventy ", "8":"eighty ", "9": "ninety "} | |
def int_to_string(num): | |
numString = str(num) | |
numLength = len(numString) | |
resultString = "" | |
teensFlag = False # A flag to signal when to use the "teen" words | |
skipThousands = False # A flag to signal when the word "thousand" gets skipped in colloquial English | |
for index, letter in enumerate(numString): | |
newIndex = numLength - index - 1 # A new index which allows us to parse the word left-to-right | |
if newIndex % 3 == 0: # retrieve ones place | |
if teensFlag == True: | |
resultString += teensDic[letter] | |
teensFlag = False | |
elif letter != "0": | |
resultString += onesDic[letter] | |
if newIndex % 3 == 1: # retrieve tenths place | |
if letter == "1": | |
teensFlag = True | |
elif letter != "0": | |
resultString += entysDic[letter] | |
if newIndex % 3 == 2: # retrieve hundredths place | |
if letter != "0": | |
resultString += onesDic[letter] + "hundred " | |
if newIndex == 3 and not skipThousands: # use word "thousand" | |
resultString += "thousand " | |
if newIndex == 6 and newIndex != 0: # use word "million" | |
resultString += "million " | |
if numString[3:6] == "000": | |
skipThousands = True | |
return resultString | |
testArray = [1, 7, 14, 86, 111, 123, 800, 702, 90134, 18001, 12015, 7790134, 11000000, 84000000, 100000000, 401019710, 876543210, 999999999] | |
for num in testArray: | |
print int_to_string(num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment