#C62. Popularity Ranking of Programming Languages
Popularity Ranking of Programming Languages
Popularity Ranking of Programming Languages
You are given several test cases. For each test case, you are provided a number n followed by n lines where each line contains a space-separated list of programming languages used in a project. For each test case, you need to compute the "popularity score" of each programming language by counting how many times it appears among the projects, and then output the languages sorted by:
- Decreasing order of their frequency (popularity score), and
- In case of ties, in lexicographical order (i.e. alphabetically ascending).
Mathematically, if f(l) denotes the frequency of language l, then the languages should be ordered so that for any two languages l1 and l2:
\[ l1 \text{ appears before } l2 \iff \Big( f(l1) > f(l2) \Big) \quad \text{ or } \quad \Big( f(l1) = f(l2) \text{ and } l1 < l2 \Big). \]The input will consist of multiple test cases. A test case starts with an integer n indicating the number of projects. The next n lines contain the programming languages used in each project. A test case with n = 0
indicates the end of input. For each test case, output the sorted list of programming languages (as a space-separated string) on a new line.
inputFormat
The input is read from stdin and is structured as follows:
- An integer n representing the number of projects in the test case.
- n lines each containing a space-separated list of programming languages.
- Multiple test cases are provided one after another. A test case starting with
0
terminates the input.
outputFormat
For each test case, output a single line (to stdout) containing the space-separated list of programming languages sorted first by their popularity (in descending order) and then lexicographically in case of ties.
## sample3
python java
java c++
python c++
0
c++ java python
</p>