v9.1.1
Menu

Character Frequency Counter

easyFREEText ProcessingNLP 1: Foundations

Given a string, count the frequency of each character (case-insensitive, ignoring spaces) and return the result as a sorted dictionary.

Example:

  • Input: "Hello World"
  • After lowering and removing spaces: "helloworld"
  • Frequencies: {'d': 1, 'e': 1, 'h': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1}

Example

Input
Hello World
Output
{'d': 1, 'e': 1, 'h': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1}
Reasoning
  • Input: text is the string Hello World: 10 letters plus one space. Capital H and W count as the same letters as h and w, and the space is not counted at all.
  • Normalising: folding case and dropping the space leaves the 10-character sequence "helloworld". Every character that remains contributes exactly 1 to the tally of its letter, so the counts sum to 10.
  • Tallying: ll appears at positions 3, 4 and 9 of "helloworld", giving 3; oo appears at positions 5 and 7, giving 2; h,e,w,r,dh, e, w, r, d each appear once. Check: 3+2+5×1=103 + 2 + 5 \times 1 = 10.
  • Output: a dictionary whose keys are the 7 distinct characters and whose values are their counts, listed in alphabetical order: {'d': 1, 'e': 1, 'h': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1}.
  • Notice: the key order follows the alphabet, not first appearance — d comes first even though it is the last letter of the input. Only spaces are ignored; punctuation or digits would get their own keys.

Constraints

  • Input: A single line of text
  • Case-insensitive (convert to lowercase)
  • Ignore spaces
  • Output: Dictionary with characters sorted alphabetically

More from NLP 1: Foundations

solution.py

Test results

Sign in to run your code against the test cases.