Spoken Amount Decoder: English Words to Signed Integer
StringsAuto-graded · 28 test casesLast updated
Parse an English number phrase (up to millions, optional "negative" prefix) into its integer value using a single left-to-right pass with group accumulators.
Spoken Amount Decoder
You are given a string text that spells out a whole number in English. Return the integer it represents.
Vocabulary
The text contains only these lowercase words, separated by single spaces:
negativezero,one,two,three,four,five,six,seven,eight,nineten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteentwenty,thirty,forty,fifty,sixty,seventy,eighty,ninetyhundred,thousand,million
Rules
- A negative value begins with the word
negative, followed by the words for its magnitude. Otherwise the value is non-negative. - Numbers are written in the standard way: groups of up to three digits, each followed by
thousandormillionwhere applicable, from largest to smallest. Groups equal to zero are omitted (for example,seven million fouris 7000004). hundredis only used within a group of 1 to 999, never to express values of 1000 or more. For instance, 1500 is writtenone thousand five hundred, notfifteen hundred.- Tens words and units words are written as separate words (for example,
thirty eight, with no hyphen). - The word
zeroonly appears when the whole number is zero.
Return the value as a Python int.
Constraints
- The result lies in the range −999,999,999 to 999,999,999.
textis non-empty, contains only the words listed above, and is separated by single spaces with no leading or trailing spaces.- The phrase is well-formed according to the rules above.
Examples
Example 1
Input: decode_spoken_number('fifteen')
Output: 15
Example 2
Input: decode_spoken_number('negative six hundred thirty eight')
Output: -638
Example 3
Input: decode_spoken_number('zero')
Output: 0
Example 4
Input: decode_spoken_number('one hundred fifty thousand two hundred')
Output: 150200
Hints
Hint 1
Think of the number as up to three groups, each worth 0 to 999, with the groups followed by a scale word (million or thousand). Which running values do you need to track?
Hint 2
Keep two accumulators: one for the group currently being built, and one for the total of groups that have been closed by a scale word. A scale word multiplies only the current group, then moves it into the total.
Hint 3
Handle the sign first (check for a leading negative word), then process the remaining words: add small-number words to the current group, multiply the current group by 100 on hundred, and flush it on thousand or million. Remember that a scale word or hundred may appear with an empty current group only if the input is unusual, so a safe fallback is to treat an empty group as 1.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
An English number phrase is a sequence of groups, each worth 0 to 999, where each group may be followed by a scale word (million or thousand). For example:
nine hundred five million | twelve thousand | three hundred forty
We scan the words once from left to right, keeping two accumulators:
current: the value of the group being built (0 to 999).total: the sum of all groups already closed by a scale word, each already multiplied by its scale.
Processing rules:
| Word | Action |
|---|---|
zero to nineteen, twenty to ninety | add its value to current |
hundred | multiply current by 100 (treating an empty group as 1) |
thousand / million | add current * scale to total (empty group treated as 1), then reset current to 0 |
leading negative | remember the sign and skip the word |
The answer is sign * (total + current).
Why it is correct
Any value up to 999,999,999 decomposes uniquely as a * 10^6 + b * 10^3 + c with 0 <= a, b, c <= 999. The phrase spells these groups in descending order, skipping zero groups. Inside a group, hundred is only applied when current holds a single digit, so the multiplication gives 100 * d; the following tens/units words are then added. A scale word closes the group, adding group * scale to total and resetting current, so a closed group is never multiplied again (this is why one million two thousand does not turn the million into a billion). Whatever remains in current at the end is the final unscaled group. The sign is applied last.
Because hundred never follows a value of 10 or more, no phrase like "fifteen hundred" arises, and the simple multiplication is sufficient.
Complexity
- Time: O(k) for k words, each handled with a dictionary lookup and constant arithmetic.
- Space: O(k) for the list of words (the dictionary is constant size).
Reference solution
def decode_spoken_number(text):
small = {
"zero": 0, "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,
}
words = text.lower().split()
sign = -1 if words and words[0] == "negative" else 1
if sign == -1:
words = words[1:]
total = 0
current = 0
for word in words:
if word in small:
current += small[word]
elif word == "hundred":
current = (current or 1) * 100
elif word == "thousand":
total += (current or 1) * 1000
current = 0
elif word == "million":
total += (current or 1) * 1_000_000
current = 0
else:
raise ValueError(f"unsupported number word: {word}")
return sign * (total + current)