FORTY TWO? SIX? NINE? Understand these and much more!

4

0

Given the written, spelled-out representation of an integer from 1 to 10^15-1 inclusive, convert it to an actual integer in decimal.

Input Format

The input will always be a string of groups, where each group is composed only of letters and spaces(). The groups are separated by commas.

You may choose whether you want the letters to be all lowercase, all uppercase, or title case.

For the purposes of this challenge, there are no hyphens (-) between e.g. "forty-two", so tokens within groups are separated by spaces only.

The input will contain no words and.

You can test if an input is valid by putting it as input into this Retina program.

Here is a full table of tokens:

Units:

+1 | "one"
+2 | "two"
+3 | "three"
+4 | "four"
+5 | "five"
+6 | "six"
+7 | "seven"
+8 | "eight"
+9 | "nine"

Teens:

+10 | "ten"
+11 | "eleven"
+12 | "twelve"
+13 | "thirteen"
+14 | "fourteen"
+15 | "fifteen"
+16 | "sixteen"
+17 | "seventeen"
+18 | "eighteen"
+19 | "nineteen

Tens:

+20 | "twenty"
+30 | "thirty"
+40 | "forty"
+50 | "fifty"
+60 | "sixty"
+70 | "seventy"
+80 | "eighty"
+90 | "ninety"

Hundred:

×100 | "hundred"

Place markings:

×10^3  | "thousand"
×10^6  | "million"
×10^9  | "billion"
×10^12 | "trillion

How to Evaluate a Spelled-out Representation

We will be working with the example input "forty five thousand,one hundred thirteen"

First, split the input into groups by commas, to get e.g. ["forty five thousand","one hundred thirteen"]

Then, evaluate each group:

  1. Split by spaces to get a list of tokens
  2. Start with a running total of 0
  3. For each token, do as the table says. If the token is 9, do +9 and add 9 to the running total. If the token is thousand, multiply the running total by 1000.

For the second example group, this would be:

running total    token
0                
1                "one"
100              "hundred"
113              "thirteen"

For the first example group, this yields 45000.

After this is done, sum each group to get the output e.g.45113.

Note that you do not have to evaluate the number exactly as I had laid it out as long as you produce the same results. For example, you could do string substitution to get an expression you can eval.

Test Cases

"one" -> 1
"ten" -> 10
"forty" -> 40
"one hundred" -> 100
"one thousand" -> 1000
"ten million" -> 10000000
"one hundred billion" -> 100000000000
"ten trillion" -> 10000000000000
"nine hundred ninety nine trillion,nine hundred ninety nine billion,nine hundred ninety nine million,nine hundred ninety nine thousand,nine hundred ninety nine" -> 999999999999999
"two hundred forty one trillion,eight hundred sixty one billion,seventy seven million,one hundred fifty four thousand,twenty eight" -> 241861077154028
"four hundred eighty five trillion,six hundred fifty seven billion,six hundred million,four hundred eighty two thousand,one hundred seventy eight" -> 485657600482178
"three hundred eighty nine trillion,five hundred twenty billion,one hundred ninety seven million,nine hundred fifty thousand,nine hundred eighty four" -> 389520197950984
"one hundred fifty five trillion,five hundred five billion,four hundred fifty eight million,six hundred seventy thousand,six hundred sixty one" -> 155505458670661
"one hundred fifty two trillion,ninety three billion,two hundred twenty one million,nine hundred thirty two thousand,five hundred eighty" -> 152093221932580
"three hundred forty three trillion,two hundred eighty six billion,nine hundred sixty five million,six hundred four thousand,two hundred seventy nine" -> 343286965604279
"six hundred ninety eight trillion,nine hundred seventy five billion,one hundred five million,sixty thousand,two hundred forty" -> 698975105060240
"five hundred seventy four trillion,one hundred sixty billion,five hundred ninety eight million,one hundred nine thousand,sixty two" -> 574160598109062
"four hundred seventy seven trillion,two hundred twenty five billion,eight hundred sixty three million,one hundred eighteen thousand,eight hundred four" -> 477225863118804
"eighty seven trillion,two hundred eighty three billion,six hundred eighty eight million,eight hundred fifty three thousand,sixty nine" -> 87283688853069
"eight hundred trillion,fifty billion,five hundred seven million,eighty thousand,fifty" -> 800050507080050
"seven hundred seventy three trillion,three hundred billion,four hundred thousand,nine" -> 773300000400009
"three hundred trillion,two hundred eighty eight billion" -> 300288000000000
"four hundred trillion,two hundred six billion,sixty six million,sixty" -> 400206066000060
"eight trillion,ninety nine million,four hundred sixty thousand,four hundred" -> 8000099460400
"seventy trillion,six hundred billion,forty million,seven hundred thousand" -> 70600040700000
"one hundred twenty billion,one hundred million,seventy thousand,forty" -> 120100070040
"four billion,eight hundred million,five hundred four thousand,five hundred six" -> 4800504506
"five hundred trillion,three hundred fifty five billion,seventy six thousand" -> 500355000076000
"fifty eight trillion,ninety eight billion,forty" -> 58098000000040

Not a Duplicate

The other question involves the units only modifying the previous value, such that every 'group' is composed of only an integer and a modifier. This requires a more dynamic approach in order to calculate groups of varying sizes.

fireflame241

Posted 2017-08-28T00:36:54.350

Reputation: 7 021

Question was closed 2017-08-28T04:33:39.827

Sandbox : Here

– fireflame241 – 2017-08-28T00:37:42.480

Er, that challenge involves interpreting units in a similar way, and then asks you to print the result with some other system. Answers to that challenger generally do the two parts seperately, which means they can be copied with the second half removed – pppery – 2017-08-28T00:52:15.353

Answers

9

Mathematica, 28 bytes

Mathematica has a builtin for everything

Interpreter@"SemanticNumber"

Of course, Mathematica has a built-in interpreter for spelled-out numbers.

Try it on Wolfram Sandbox

Usage

f=Interpreter@"SemanticNumber"

 

f["three hundred trillion,two hundred eighty eight billion"]
300288000000000
f["five hundred seventy four trillion,one hundred sixty billion,five hundred ninety eight million,one hundred nine thousand,sixty two"]
574160598109062

JungHwan Min

Posted 2017-08-28T00:36:54.350

Reputation: 13 290

5I really don't like these answers, but I won't downvote it as there's nothing wrong with it. – Stan Strum – 2017-08-28T01:00:40.250

5@StanStrum uh... okay? I think your comment is more suitable in chat (or meta posts), not comments. It doesn't add any value to the answer. – JungHwan Min – 2017-08-28T01:02:02.650

2

Python 2, 491 bytes

a,m,s,r={'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10,'eleven':11,'twelve':12,'thirteen':13,'fourteen':14,'fifteen':15,'sixteen':16,'seventeen':17,'eighteen':18,'nineteen':19,'twenty':20,'thirty':30,'forty':40,'fifty':50,'sixty':60,'seventy':70,'eighty':80,'ninety':90},{'hundred':100,'thousand':1e3,'million':1e6,'billion':1e9,'trillion':1e12},input(),0
for i in s.split(','):
 j=0
 for k in i.split():j=j+a[k]if k in a else j*m[k]
 r+=j
print r

Try it online!

totallyhuman

Posted 2017-08-28T00:36:54.350

Reputation: 15 378

1You can save some bytes by writing r as dict(one=1... – pppery – 2017-08-28T02:27:55.263