Valid Decimal Literal
Return True if s is a valid decimal literal, otherwise False. A valid decimal literal is, in this order:
- an optional sign,
+or-; - then a run of digits, optionally followed by a
.and another run of digits, where at least one of the two runs must be non-empty (so12,12.,.5and12.5are all fine, but.on its own is not); - then an optional exponent: the letter
eorE, an optional sign, and a run of one or more digits.
Nothing else may appear: no spaces, no second sign, no second dot and no other letters. For example -1.5e10, +.5, 3. and 0e0 are valid, while e5, 1e, 1.2.3, --1, 1 and the empty string are not.
Example 1
s "-1.5e10"
returns true
A sign, digits on both sides of the dot, and an exponent with digits.
Example 2
s "+.5"
returns true
The digits before the dot may be missing when there are digits after it.
Example 3
s "1e"
returns false
The exponent marker must be followed by at least one digit.
Constraints
0 <= len(s) <= 20shas digits, letters, spaces and the symbols+,-,.only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.