Validate a Battle Score Display
Context
A game stores score displays as text before showing them on a level board. Accept only nonnegative decimal score text: a nonempty run of digits, optionally followed by a decimal point and one or two digits. Reject signs, spaces, missing integer digits, missing fractional digits, extra separators, and excessive precision.
Problem
Write a function that decides whether score_text follows the game’s score-display format exactly. The text must contain one or more digits for the whole-number portion. It may stop there, or continue with one decimal point followed by exactly one or two digits. No other characters are allowed, and the entire string must conform. Do not convert the text to a numeric value; validate its characters and structure. Return true only for an accepted score display and false otherwise.
Examples
score_text = "7"Truescore_text = "42.5"Truescore_text = "103.25"TrueConstraints
1 <= len(score_text) <= 40score_text uses digits only for its numeric portions and may also contain one optional decimal point.- Types:
score_textis str; result is bool
Function signature
def is_valid_score_display(score_text: str) -> bool
score_textstr- The score display text to validate.
- returns bool
- True exactly when the complete text is an unsigned decimal score with an optional one- or two-digit fractional part; otherwise false.
Adapted from MBPP problem task_759 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Validation must preserve the original text and must not rely on numeric conversion.
- The decimal point is permitted only between a nonempty integer portion and a nonempty fractional portion.