PracticeStrings

The Three-Class Character Tally

BeginnerStringstraversalaccumulationstate tracking Time not estimated Not started

Context

A finite string is viewed as a sequence of symbols. Each symbol belongs to exactly one of three classes: alphabetic characters, decimal digits, or all remaining characters. Determine how many symbols belong to each class.

Problem

Write a function that examines every character in symbols exactly once and counts the characters in three disjoint classes. An alphabetic character is an uppercase or lowercase English letter. A numeric character is one of the decimal digits 0 through 9. Every other character, including whitespace and punctuation, belongs to the remaining class. Return an integer list containing the totals in this order: alphabetic, numeric, other. Repeated characters are counted separately, and an empty string produces three zero totals.

Examples

Example 1
Input: symbols = "Hello, World! 123"
Output: [10, 3, 4]
Explanation: For "Hello, World! 123", the executed output is [10, 3, 4], in alphabetic, numeric, other order.
Example 2
Input: symbols = "A1-bB2!!"
Output: [3, 2, 3]
Explanation: For "A1-bB2!!", the executed output is [3, 2, 3], in alphabetic, numeric, other order.
Example 3
Input: symbols = " 007 #"
Output: [0, 3, 5]
Explanation: For " 007 #", the executed output is [0, 3, 5], in alphabetic, numeric, other order.

Constraints

  • 0 <= len(symbols) <= 100000
  • symbols: alphanumeric
  • spaces and punctuation may also occur.
  • Types: symbols is str; result is int[]

Function signature

def count_character_classes(symbols: str) -> list[int]
symbols str
The string whose characters are classified and counted.
returns list[int]
Three integers in order: the number of alphabetic characters, numeric characters, and other characters.

Adapted from MBPP problem task_828 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · c035r1-abs_228_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1