GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/273_Integer_to_English_Words.py at master · qiyuangong/leetcode

Expand file tree

/

273_Integer_to_English_Words.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
17 lines (17 loc) · 903 Bytes
/

273_Integer_to_English_Words.py

Copy path

File metadata and controls

17 lines (17 loc) · 903 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.