PIXELBANKv9.1.0
Menu

Extract numerical features from text for classification.

Given a text, compute these features:

  1. word_count — number of words
  2. char_count — number of characters (excluding spaces)
  3. avg_word_length — average word length, rounded to 4 decimal places
  4. vocab_richness — number of unique words / total words, rounded to 4 decimal places
  5. longest_word — length of the longest word
  6. digit_ratio — fraction of characters that are digits (of all non-space chars), rounded to 4 decimal places

Input: A single line of text (may contain letters, digits, spaces)

Output: A dictionary with the six features, printed with keys in the order listed above.

Example:

Input:
hello world hello 123
Output:
{'word_count': 4, 'char_count': 17, 'avg_word_length': 4.25, 'vocab_richness': 0.75, 'longest_word': 5, 'digit_ratio': 0.1765}
Reasoning:

word_count: 4 words: ["hello", "world", "hello", "123"]

char_count: h-e-l-l-o-w-o-r-l-d-h-e-l-l-o-1-2-3 = 17 chars (no spaces)

avg_word_length: (5 + 5 + 5 + 3) / 4 = 18/4 = 4.25

vocab_richness: 3 unique words / 4 total = 0.75

longest_word: max(5, 5, 5, 3) = 5

digit_ratio: 3 digits out of 17 non-space chars = 3/17 = 0.1765

Constraints:

  • Words are split on whitespace
  • Character count excludes spaces
  • Word comparisons for vocab_richness are case-sensitive
  • Round float values to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Text Feature Extractor - Medium | PixelBank