File: check-if-numbers-are-ascending-in-a-sentence/solution.py

Date: 2026-06-06

Time: 15:41

Purpose

This file implements LeetCode 2042: Check if Numbers Are Ascending in a Sentence. It contains both the solution and its unit tests in a single module — the standard layout across this repository. The responsibility is narrow: given a sentence string containing words and numeric tokens, determine whether all numbers appear in strictly increasing order from left to right.

Key Components

Solution.areNumbersAscending(self, s: str) -> bool

The core algorithm. Splits the sentence on whitespace, filters for numeric tokens via str.isdigit(), and checks strict monotonicity by tracking a running prev value initialized to -1.

Contract: Accepts a space-separated sentence where numbers are contiguous digit tokens. Returns True if every numeric token is strictly greater than all preceding numeric tokens, False otherwise. Non-numeric tokens are ignored entirely.

TestAreNumbersAscending

Eight test cases covering the LeetCode examples plus edge cases: two-number ascending/descending, all-number sequences, boundary values (1 and 99), and leading-number descending sequences.

Patterns

Dependencies

Imports: Only unittest from the standard library — no external dependencies.

Imported by: The "Imported By" list in the prompt is misleading — it shows ~400+ test files. This is almost certainly an artifact of the analysis tool conflating import unittest across the repo, not actual imports of this module. No other solution depends on this file's Solution class.

Flow

1. s.split() tokenizes the sentence into a list of whitespace-delimited strings.

2. Each token is tested with isdigit() — only purely numeric strings pass.

3. Numeric tokens are converted to int and compared against prev.

4. If any number is <= the previous one, short-circuit return False.

5. Otherwise update prev and continue. If the loop completes, return True.

The scan is single-pass O(n) where n is the length of the string, with O(n) space for the split result.

Invariants

Error Handling

None — the function assumes well-formed input per the LeetCode contract. No validation of the input string, no handling of empty strings, no protection against tokens like "007" (which isdigit() would accept and int() would parse as 7). For LeetCode's constraints this is correct; the problem guarantees tokens are either lowercase English words or numbers without leading zeros.

Topics to Explore

Beliefs