KatInterview

Check Whether a Token Is a Well-Formed Decimal Literal

StringsAuto-graded · 41 test cases
MediumAuto-gradedStringsstring-parsingstate-machinevalidation

Last updated

Scan a string once to decide if it matches a small decimal-literal grammar with optional sign, a dotted mantissa and an optional exponent, handling many edge cases.

Decimal Literal Checker

You are given a string token made of ASCII characters. Decide whether it is a well-formed decimal literal according to the rules below, and return True if it is and False otherwise.

Format

A literal has up to three parts, written in this order:

  1. Sign (optional). A single + or - at the very start.
  2. Mantissa (required). A run of decimal digits that may contain at most one decimal point ., which can be at the start, in the middle, or at the end of the run. The mantissa must contain at least one digit. A lone . is therefore not a mantissa.
  3. Exponent (optional). The letter e or E, then an optional + or -, then one or more digits. The exponent part is an integer only, so it cannot contain a decimal point.

Additional rules

  • The whole string must match the format. No spaces, commas, underscores, letters other than the exponent marker, or any other characters are allowed anywhere.
  • Leading zeros are fine.
  • Only one sign may appear before the mantissa, and only one decimal point may appear in the mantissa.
  • Only digits 0 through 9 count as digits.
  • The empty string is not valid.

For example, strings such as e3, +., 1.2.3, --6, 6e and 6e- are all invalid.

Task

Implement the function so that it returns True exactly when token is a well-formed decimal literal under these rules.

Constraints

  • 0 <= len(token) <= 10^5
  • token consists of ASCII characters.
  • Do not rely on float() conversion to decide validity.
  • Aim for linear time and constant extra space.

Examples

Example 1

Input: is_decimal_literal('123')

Output: True

Example 2

Input: is_decimal_literal('0.5')

Output: True

Example 3

Input: is_decimal_literal('.8')

Output: True

Example 4

Input: is_decimal_literal('10.')

Output: True

Example 5

Input: is_decimal_literal('1e9')

Output: True

Example 6

Input: is_decimal_literal('-3.2E-4')

Output: True

Example 7

Input: is_decimal_literal('e3')

Output: False

Example 8

Input: is_decimal_literal('+.')

Output: False

Example 9

Input: is_decimal_literal('1.2.3')

Output: False

Example 10

Input: is_decimal_literal('--6')

Output: False

Example 11

Input: is_decimal_literal('6e')

Output: False

Example 12

Input: is_decimal_literal('6e-')

Output: False

Hints

Hint 1

The format has no nesting, so a single left-to-right pass with one moving index is enough. Handle the parts in order: sign, mantissa, exponent.

Hint 2

In the mantissa, count digits and remember whether a dot was already seen. After the loop, require at least one digit. This one check rejects '.', '+.', '-' and 'e3'.

Hint 3

After an optional exponent marker and optional sign, require at least one exponent digit. At the end, return whether the index reached the end of the string, which catches any leftover characters such as a second dot or sign.