Numerals and Parsing
What is this?
This is about translating numbers to and from the ways humans write them โ Roman numerals like MCMXCIV, the English words "one thousand two hundred", or a decimal you read off a string. Each notation is really just a small dictionary of symbols plus rules for how they combine. Parsing also means reading messy real-world text and pulling a clean number out of it.
๐ก Fun fact: A repeating decimal like 1/7 = 0.142857142857... never actually ends, but you can detect the loop in code by remembering each remainder you have already seen โ the moment one repeats, you have found the cycle.
๐ The 5 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: human number notations are just lookup tables plus rules for grouping and ordering, so converting them is a matter of greedily matching the largest piece, walking a remainder, and guarding the messy edge cases.
The pattern
Each problem here pairs a fixed vocabulary with a strategy for consuming or producing it. The recurring moves are: keep a value table sorted from largest to smallest and greedily subtract or emit the biggest piece that fits; group digits into fixed-size chunks so a small rule set covers an unbounded range; and run a state machine when raw text has optional signs, whitespace, or trailing junk.
The reason greedy works for these notations is that they are designed to be unambiguous from the top down โ the largest applicable symbol is always part of a valid representation. The reason parsing is fiddly is the opposite: real input is messy, and the hard work is rejecting or clamping what does not fit rather than the conversion itself. Long division adds a third idea โ track remainders to detect when a process repeats.
The problems
Integer to Roman uses a greedy value table that already includes the subtractive forms (4, 9, 40, 90, ...). Walk the table high to low, appending each symbol while it fits and subtracting its value, so the awkward cases need no special handling.
Roman to Integer scans left to right, adding each symbol's value but subtracting when a smaller symbol precedes a larger one โ that lone comparison captures every subtractive pair.
Integer to English Words groups the digits into threes (thousand, million, billion), names each group with one helper, and stitches the scale words between them โ turning an open-ended range into a tiny rule set.
Fraction to Recurring Decimal is long division: emit the integer part, then repeatedly multiply the remainder, recording each remainder in a map; the first repeat marks where the recurring block begins.
String to Integer (atoi) is a state machine: skip leading spaces, read an optional sign, consume digits, stop at the first non-digit, and clamp to the integer range on overflow.
Key takeaways
- A largest-first value table makes Roman conversions a clean greedy loop in both directions.
- Grouping digits into threes collapses arbitrarily large numbers into a handful of naming rules.
- Long division detects repetition by remembering remainders; a repeated remainder is a repeating decimal.
- Robust parsing is mostly edge-case discipline: signs, whitespace, trailing characters, and overflow clamping.
Roman to Integer
Core idea: Walk left to right; if a symbol is smaller than the one to its right, subtract it โ otherwise add it. That single comparison captures every Roman numeral rule.
Problem, rephrased
You're restoring an old museum catalogue. The ledger numbers chapters with Roman numerals โ I, V, X, L, C, D, M โ and you need to turn each label back into an ordinary integer so the digital index can sort them.
The seven symbols map to fixed values:
| Symbol | Value |
|---|---|
I |
1 |
V |
5 |
X |
10 |
L |
50 |
C |
100 |
D |
500 |
M |
1000 |
Numerals normally read from largest on the left to smallest on the right, and you simply add the pieces. The one twist: when a smaller value sits directly before a larger one, that small value is subtracted instead of added. So IV is 5 โ 1 = 4, and XL is 50 โ 10 = 40.
Inputs and outputs:
| Input | Output | Why |
|---|---|---|
"III" |
3 | 1 + 1 + 1 |
"XIV" |
14 | 10 + (5 โ 1) |
"XLII" |
42 | (50 โ 10) + 1 + 1 |
"MCMXCIV" |
1994 | 1000 + (1000 โ 100) + (100 โ 10) + (5 โ 1) |
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson