Validate a Plain Signed Decimal Literal
StringsAuto-graded · 25 test casesLast updated
Check whether a string is a well-formed plain decimal literal (optional sign, digits, optional fraction) by turning a small grammar into a careful single-pass validator.
Write a function that reports whether a given string is a well-formed decimal literal in plain notation. Only a sign, digits, and a single fractional part are allowed. Exponents and any other symbols are not.
Task
Given a string text, return True if the entire string follows the pattern below, and False otherwise.
Format
A valid string consists of these parts, in this order, with nothing before or after:
- An optional sign: a single
+or-. - The integer part: one or more decimal digits.
- An optional fractional part: one
.followed by one or more decimal digits.
Implications
"234","-455", and"23.23"are valid."1e2"is invalid (no exponent notation), and"12^2"is invalid (^is not permitted)."2.2.4"is invalid because it contains more than one decimal point.- Leading zeros are fine:
"007"and"-0"are valid. - These are all invalid:
"","-",".5","5.", and any string that contains whitespace.
Allowing a leading +, allowing leading zeros, and demanding digits on both sides of the decimal point are rules of this exercise.
Constraints
0 <= len(text) <= 100textconsists of printable characters, spaces included.- A "digit" is any character for which Python's
str.isdigit()returnsTrue(for ordinary ASCII input this is exactly0through9). - Return a Python
bool.
Examples
Example 1
Input: is_plain_decimal('-455')
Output: True
Example 2
Input: is_plain_decimal('23.23')
Output: True
Example 3
Input: is_plain_decimal('2.2.4')
Output: False
Example 4
Input: is_plain_decimal('234')
Output: True
Hints
Hint 1
Write the format as a tiny grammar first: optional sign, then at least one digit, then optionally a dot followed by at least one digit.
Hint 2
Scan with a single index. Consume the sign if present, then a run of digits, then the optional dot and its run of digits. Each run must be non-empty where the format requires it.
Hint 3
After the scan, the whole string must be consumed. Leftover characters (a second dot, a letter, a space) mean the answer is False.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The valid strings follow the pattern [+-]? digit+ ( . digit+ )?. We check this with one left-to-right pass using an index i:
- If the string is empty, return
False. If the character atiis+or-, skip it. - Require at least one digit at
i, then skip the whole run of digits. If there is no digit, returnFalse. - If the next character is
., skip it, require at least one digit, and skip that run of digits. If there is no digit after the dot, returnFalse. - Return
Trueonly ifihas reached the end of the string.
Why it is correct
Every step is greedy and never needs to backtrack: a sign can only be at position 0, and a digit run ends exactly at the first non-digit.
- A second sign (
"+-5") fails step 2 because a non-digit appears where a digit is required. - The empty string, a lone sign,
".5", and"-.5"all fail because no integer digits are found. "5."and"-3."fail step 3 because the fraction is empty."2.2.4","1e2","12^2", and trailing spaces leave unconsumed characters, so the final check fails. A leading space fails step 2.- Leading zeros are just digits, so
"007"and"-0"pass.
Any string matching the pattern passes every step and finishes with i == n, so the function returns True exactly for the valid strings.
The digit test uses str.isdigit(), which matches 0 to 9 for ordinary ASCII input and keeps the behavior consistent with the expected outputs on every test.
Complexity
- Time: O(n), since each character is looked at at most once.
- Space: O(1), since only an index is kept.
Reference solution
def is_plain_decimal(text: str) -> bool:
if not text:
return False
n = len(text)
i = 0
# Optional sign
if text[i] in ('+', '-'):
i += 1
# Integer part: at least one digit
if i >= n or not text[i].isdigit():
return False
while i < n and text[i].isdigit():
i += 1
# Optional fractional part: '.' followed by at least one digit
if i < n and text[i] == '.':
i += 1
if i >= n or not text[i].isdigit():
return False
while i < n and text[i].isdigit():
i += 1
# Entire string must be consumed
return i == n