GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/273_Integer_to_English_Words.py at master · qiyuangong/leetcode Expand file tree/ Copy path273_Integer_to_English_Words.pyMore file actions 17 lines (17 loc) · 903 Bytes / Copy path273_Integer_to_English_Words.pyFile metadata and controls17 lines (17 loc) · 903 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution(object): def numberToWords(self, num): # https://leetcode.com/problems/integer-to-english-words/discuss/70632/Recursive-Python to19 = 'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve ' \ 'Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split() tens = 'Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split() def words(n): if n < 20: return to19[n - 1:n] if n < 100: return [tens[n / 10 - 2]] + words(n % 10) if n < 1000: return [to19[n / 100 - 1]] + ['Hundred'] + words(n % 100) for p, w in enumerate(('Thousand', 'Million', 'Billion'), 1): if n < 1000 ** (p + 1): return words(n / 1000 ** p) + [w] + words(n % 1000 ** p) return ' '.join(words(num)) or 'Zero' |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |