-2

I am trying to understand the code from Is there a way to convert number words to Integers?. I could not understand the below statement:

for idx, word in enumerate(scales):   numwords[word] = (10 ** (idx * 3 or 2), 0)

Below is the code:

def text2int(textnum, numwords={}):
    if not numwords:
      units = [
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
        "sixteen", "seventeen", "eighteen", "nineteen",
      ]

      tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

      scales = ["hundred", "thousand", "million", "billion", "trillion"]

      numwords["and"] = (1, 0)
      for idx, word in enumerate(units):    numwords[word] = (1, idx)
      for idx, word in enumerate(tens):     numwords[word] = (1, idx * 10)
      for idx, word in enumerate(scales):   numwords[word] = (10 ** (idx * 3 or 2), 0)

    current = result = 0
    for word in textnum.split():
        if word not in numwords:
          raise Exception("Illegal word: " + word)

        scale, increment = numwords[word]
        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0

    return result + current

print text2int("seven billion one hundred million thirty one thousand three hundred thirty seven")
#7100031337
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Samir Tendulkar
  • 1,151
  • 3
  • 19
  • 47
  • Which part precisely do you not understand? – jonrsharpe Sep 06 '18 at 22:22
  • @jonrsharpe `for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)` I have put this in the question and the main title too – Samir Tendulkar Sep 06 '18 at 22:28
  • Yes, but that's two whole lines. Be more specific. Which parts of it *do* you understand? Have you tried experimenting with them in isolation and changing things to see what happens? Read the docs related to the function being called? – jonrsharpe Sep 06 '18 at 22:28
  • @jonrsharpe `(10 ** (idx * 3 or 2), 0)` I got the forloop part I also understand that code is printing the index of the strings above – Samir Tendulkar Sep 06 '18 at 22:31
  • So [edit] the question - that's creating a tuple, have you experimented with different values of `idx`? – jonrsharpe Sep 06 '18 at 22:34
  • I could not as I did not understand it. Specially the `or` what exactly is it doing – Samir Tendulkar Sep 06 '18 at 22:36
  • 1
    Why does not understanding it prevent you just running it and seeing what happens? The `or` does what it usually does - gives you the left-hand value if it's truthy, the right-hand value otherwise. – jonrsharpe Sep 06 '18 at 22:38

1 Answers1

0

From what I understand, it is getting adding on 10^(3x) for each scale word (million,billion,trillion) it can find based on where it lands in that array, of which I have denoted with an x.

Hunter
  • 201
  • 3
  • 17
  • could you explain more simply on how it accounts for hundred and thousand what does the `or` do – Samir Tendulkar Sep 06 '18 at 22:34
  • The hundred and the thousand works in the same way, those were just examples. The or seems to have some effect on the first three digits of the number. – Hunter Sep 07 '18 at 02:24