#C12140. Character Frequency Analysis

    ID: 41535 Type: Default 1000ms 256MiB

Character Frequency Analysis

Character Frequency Analysis

Given a string s consisting of various characters, your task is to compute the frequency of each unique character and output a list of tuples. Each tuple contains a character and its count. The list must be sorted primarily in descending order by frequency and, in the case of a tie, in alphabetical order (i.e. ascending order by the character).

Sorting Rule:

Let \(f(c)\) denote the frequency of character \(c\). For two characters \(a\) and \(b\), the order is defined as:

[ \text{if } f(a) > f(b) \text{ then } a \text{ comes before } b, ]

[ \text{if } f(a) = f(b) \text{ then order by } a < b. ]

Input will be provided via standard input (stdin) and the output must be printed to standard output (stdout) using the format shown in the examples.

Examples:

  • Input: "abracadabra" → Output: [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]
  • Input: "abc" → Output: [('a', 1), ('b', 1), ('c', 1)]
  • Input: "a1b2c3!" → Output: [('!', 1), ('1', 1), ('2', 1), ('3', 1), ('a', 1), ('b', 1), ('c', 1)]

inputFormat

The input consists of a single line containing a string s. The string may include letters, digits, special characters, and spaces. It is provided via standard input (stdin).

outputFormat

Print the list of tuples where each tuple is in the format ('character', count). The list should be formatted exactly as shown in the examples, with the tuples separated by commas and enclosed within square brackets. The output should be sent to standard output (stdout).

## sample
abracadabra
[('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]