CODE
class Solution:
def numberToWords(self, num: int) -> str:
string = str(num)
rev_str = string[::-1]
str_list = []
i = 0
while i < len(rev_str):
if rev_str[i] == "0":
str_list.append("")
elif rev_str[i] == "1":
str_list.append("One")
elif rev_str[i] == "2":
str_list.append("Two")
elif rev_str[i] == "3":
str_list.append("Three")
elif rev_str[i] == "4":
str_list.append("Four")
elif rev_str[i] == "5":
str_list.append("Five")
elif rev_str[i] == "6":
str_list.append("Six")
elif rev_str[i] == "7":
str_list.append("Seven")
elif rev_str[i] == "8":
str_list.append("Eight")
elif rev_str[i] == "9":
str_list.append("Nine")
i += 1
# print(str_list)
i = 1
while i < len(rev_str):
if rev_str[i] == "0":
str_list[i] = ""
elif rev_str[i] == "1":
if rev_str[i-1] == "0":
str_list[i] = "Ten"
elif rev_str[i-1] == "1":
str_list[i] = "Eleven"
elif rev_str[i-1] == "2":
str_list[i] = "Twelve"
elif rev_str[i-1] == "3":
str_list[i] = "Thirteen"
elif rev_str[i-1] == "4":
str_list[i] = "Fourteen"
elif rev_str[i-1] == "5":
str_list[i] = "Fifteen"
elif rev_str[i-1] == "6":
str_list[i] = "Sixteen"
elif rev_str[i-1] == "7":
str_list[i] = "Seventeen"
elif rev_str[i-1] == "8":
str_list[i] = "Eighteen"
elif rev_str[i-1] == "9":
str_list[i] = "Nineteen"
str_list[i-1] = ""
elif rev_str[i] == "2":
str_list[i] = "Twenty"
elif rev_str[i] == "3":
str_list[i] = "Thirty"
elif rev_str[i] == "4":
str_list[i] = "Forty"
elif rev_str[i] == "5":
str_list[i] = "Fifty"
elif rev_str[i] == "6":
str_list[i] = "Sixty"
elif rev_str[i] == "7":
str_list[i] = "Seventy"
elif rev_str[i] == "8":
str_list[i] = "Eighty"
elif rev_str[i] == "9":
str_list[i] = "Ninety"
i += 3
# print(str_list)
i = 3
while i < len(str_list):
if num > 99:
if i % 3 == 0:
str_list[i-1] = str(str_list[i-1]) + " Hundred"
i += 3
# print(str_list)
i = 0
while i < len(str_list):
if i == 3:
str_list[i] = str(str_list[i]) + " Thousand"
elif i == 6:
str_list[i] = str(str_list[i]) + " Million"
elif i == 9:
str_list[i] = str(str_list[i]) + " Billion"
i += 1
# print(str_list)
xray = []
for i in str_list:
if i != "":
xray.append(i)
# print(x)
xray = xray[::-1]
print(xray)
if num < 1000: xray[0] += " Hundred"
answer = " ".join(xray)
# print("["+answer+"]")
answer = answer.replace(" ", " ")
if answer == "":
return "Zero"
# return "["+answer+"]"
return answer
Code explanation: basically the code is supposed to turn integers into English so:
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
the way i approached this solution was to first turn all the numbers into a list of words, the reverse the list, then turn every 3rd-1 index in to a teen word and then put either thousand, million, billion every 3rd index.
Error on LeetCode:
IndexError: list index out of range
if num < 1000: xray[0] += " Hundred"
Line 105 in numberToWords (Solution.py)
ret = Solution().numberToWords(param_1)
Line 134 in _driver (Solution.py)
_driver()
Line 145 in <module> (Solution.py)
Locally when I run something like:
if __name__ == "__main__":
s = Solution()
print(s.numberToWords(123))
i get the answer
['One', 'Twenty', 'Three']
One Hundred Twenty Three
which is expected, but i get the error above on leetcode.