SHARE
TWEET

Untitled

a guest Aug 25th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mode = 2
  2.  
  3. # Convert integer to words.
  4. if mode == 0:
  5.     units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
  6.         'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
  7.         'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
  8.     tens = ['zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
  9.         'seventy', 'eighty', 'ninety']
  10.     powers = ['zero', 'thousand', 'million', 'billion', 'trillion',
  11.         'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion',
  12.         'nonillion', 'decillion']
  13.     hundred = 'hundred'
  14.     minus = 'minus'
  15.     comma = ','
  16.     and_ = ' and'
  17.     space = ' '
  18.     hyphen = '-'
  19.     empty = ''
  20.     key = len
  21.  
  22. # Convert integer to number-of-words.
  23. elif mode == 1:
  24.     units = [1 for n in range(20)]
  25.     tens = [1 for n in range(0, 100, 10)]
  26.     powers = [1 for n in range(12)]
  27.     hundred = 1
  28.     minus = 1
  29.     comma = 0
  30.     and_ = 1
  31.     space = 0
  32.     hyphen = 0
  33.     empty = 0
  34.     key = lambda n: n
  35.  
  36. # Convert integer to number-of-syllables.
  37. elif mode == 2:
  38.     units = [2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 3, 1, 2, 2, 2, 2, 3, 2, 2]
  39.     tens = [2, 1, 2, 2, 2, 2, 2, 3, 2, 2]
  40.     powers = [2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3]
  41.     hundred = 2
  42.     minus = 2
  43.     comma = 0
  44.     and_ = 1
  45.     space = 0
  46.     hyphen = 0
  47.     empty = 0
  48.     key = lambda n: n
  49.  
  50. def small_integer_name(n, use_and=False):
  51.     s = empty
  52.     if n >= 100:
  53.         q, n = divmod(n, 100)
  54.         s = units[q] + space + hundred + (
  55.             (and_ if use_and else empty) + space if n > 0 else empty)
  56.     if n >= 20:
  57.         q, n = divmod(n, 10)
  58.         s += tens[q] + (hyphen if n > 0 else empty)
  59.     return (s + units[n] if n > 0 else s)
  60.  
  61. def integer_name(n, use_comma=False, use_and=False, power=0):
  62.     if n < 0:
  63.         return minus + space + integer_name(-n, use_comma, use_and)
  64.     elif n == 0:
  65.         return units[0]
  66.     s = empty
  67.     if n >= 1000:
  68.         q, n = divmod(n, 1000)
  69.         s = integer_name(q, use_comma, use_and, power + 1) + (
  70.             (comma if use_comma else empty) + space if n > 0 else empty)
  71.     return (s + small_integer_name(n, use_and) +
  72.             (space + powers[power] if power > 0 else empty) if n > 0 else s)
  73.  
  74. print(sum(integer_name(n) for n in range(1, 1000001)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top