Leetcode: Python3 - Integer to English solution - working locally but giving error on submission

Viewed 32

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.

1 Answers

I could not identify the error. But if you allow some suggestions.

You can use a dictionary to map numbers to words and use recursion to solve the problem and make the code less repetitive.

Example:

class Solution:
    def numberToWords(self, num):
        if num == 0:
            return 'Zero'
        d = {0: '', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 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'}
        res = ''
        if num >= 1000000000:
            res += self.numberToWords(num // 1000000000) + ' Billion'
            num %= 1000000000
        if num >= 1000000:
            res += ' ' + self.numberToWords(num // 1000000) + ' Million'
            num %= 1000000
        if num >= 1000:
            res += ' ' + self.numberToWords(num // 1000) + ' Thousand'
            num %= 1000
        if num >= 100:
            res += ' ' + self.numberToWords(num // 100) + ' Hundred'
            num %= 100
        if num > 0:
            if num <= 20:
                res += ' ' + d[num]
            else:
                res += ' ' + d[num // 10 * 10] + ' ' + d[num % 10]
        return res.strip() 
Related