Tally Clean Tokens in a Space-Delimited Line
StringsAuto-graded · 25 test casesLast updated
Split a line on spaces and count the tokens made only of ASCII letters and the marks , . ? !, testing exact tokenization and character-class checks.
Tally Clean Tokens in a Space-Delimited Line
You are given a line of text. Break it into tokens at the spaces, then decide which tokens are "clean" according to a strict character rule. Return how many clean tokens the line contains.
Definitions
- A token is a maximal run of consecutive non-space characters. Spaces at the start, at the end, or several in a row never create empty tokens.
- A token is clean if every one of its characters is one of the following:
- an English letter,
a-zorA-Z - a comma
, - a period
. - a question mark
? - an exclamation mark
!
- an English letter,
- Where the punctuation sits inside a token is irrelevant.
he?,?he,h?eand!!are all clean. - A token is not clean if it contains a digit or any other character, for example an apostrophe, hyphen, bracket, colon, quotation mark, underscore or
@. - A token consisting only of allowed punctuation, such as
,or!!, is clean.
Task
Implement tally_clean_tokens(line) that returns the number of clean tokens in line. If line is empty or contains only spaces, return 0.
Constraints
0 <= len(line) <= 10^5- Every character of
lineis printable ASCII (codes 32 to 126), so the space is the only whitespace character.
Examples
Example 1
Input: tally_clean_tokens('He has 1 million do[[ars, isnt he?')
Output: 5
Example 2
Input: tally_clean_tokens('Wait!! Is it 9am? Yes, it is.')
Output: 6
Example 3
Input: tally_clean_tokens(' don\'t use e-mail , ok ')
Output: 3
Hints
Hint 1
First turn the line into tokens. Think about what happens with leading, trailing and repeated spaces, and make sure empty pieces are never counted.
Hint 2
Rather than listing forbidden characters, define the set of allowed characters (52 letters plus , . ? !) and check each character of a token against it.
Hint 3
Do not add rules about punctuation position or count. A token is clean exactly when all of its characters are in the allowed set, including tokens made only of punctuation.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem is a filter followed by a count, done in two steps.
- Tokenize. Split the line on single spaces. Leading, trailing or repeated spaces produce empty strings, so skip any empty piece. What remains is exactly the set of maximal runs of non-space characters.
- Validate with a whitelist. Build a set containing the 52 ASCII letters and
,.?!. A token is clean only if every character is in that set. Digits and every other symbol are automatically rejected, and a token made only of punctuation is accepted.
The answer is the number of tokens that pass.
Why it is correct
- Skipping empty pieces after
split(' ')leaves precisely the maximal non-space runs, which is the definition of a token. An empty or all-space line yields no tokens, so the result is0. - Using an allowed-set check means any character outside the set, whether a digit, apostrophe, hyphen, bracket or anything else, disqualifies the token, matching the rule "anything else is not allowed".
- No position rules are applied, which matches the statement: punctuation may appear anywhere in a clean token.
Complexity
- Time: O(n), where n is
len(line). Each character is examined a constant number of times, with O(1) set lookups. - Space: O(n) for the list produced by
split. The whitelist has constant size. A single-pass scan with an "in token" flag and a "still valid" flag would bring extra space down to O(1).
Reference solution
def tally_clean_tokens(line: str) -> int:
# Whitelist: 52 ASCII letters plus the four allowed punctuation marks.
allowed = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.?!')
count = 0
# split(' ') yields empty strings for leading/trailing/repeated spaces;
# the `tok` check skips them.
for tok in line.split(' '):
if tok and all(ch in allowed for ch in tok):
count += 1
return count