PracticeStrings

Validate a Battle Score Display

Easy/MediumStringstraversalstate trackingcomparison Time not estimated Not started

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

Example 1
Input: score_text = "7"
Output: True
Explanation: The input "7" returns true, and its final traced state has position 1, text length 1, match state 1, and validity_flag true.
Example 2
Input: score_text = "42.5"
Output: True
Explanation: The input "42.5" returns true, and its final traced state has position 4, text length 4, match state 3, and validity_flag true.
Example 3
Input: score_text = "103.25"
Output: True
Explanation: The input "103.25" returns true, and its final traced state has position 6, text length 6, match state 4, and validity_flag true.

Constraints

  • 1 <= len(score_text) <= 40
  • score_text uses digits only for its numeric portions and may also contain one optional decimal point.
  • Types: score_text is str; result is bool

Function signature

def is_valid_score_display(score_text: str) -> bool
score_text str
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.
Approved · cc007r1-abs_159_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1