Integer to English Words LeetCode solution in Python
1class Solution:2def numberToWords(self, num: int) -> str:34if num == 0:5return "Zero"67dic1 = {1000000000: "Billion", 1000000: "Million", 1000: "Thousand", 1: ""}8dic2 = {990: "Ninety",1080: "Eighty",1170: "Seventy",1260: "Sixty",1350: "Fifty",1440: "Forty",1530: "Thirty",1620: "Twenty",1719: "Nineteen",1818: "Eighteen",1917: "Seventeen",2016: "Sixteen",2115: "Fifteen",2214: "Fourteen",2313: "Thirteen",2412: "Twelve",2511: "Eleven",2610: "Ten",279: "Nine",288: "Eight",297: "Seven",306: "Six",315: "Five",324: "Four",333: "Three",342: "Two",351: "One",36}3738def construct_num(num):39ans = ""40d, num = divmod(num, 100)41if d > 0:42ans += dic2[d] + " " + "Hundred"43for k, v in dic2.items():44d, num = divmod(num, k)45if d > 0:46ans += " " + v47return ans.lstrip()4849ans = ""50for k, v in dic1.items():51d, num = divmod(num, k)52if d > 0:53ans += " " + construct_num(d) + " " + v5455return ans.strip()