Top contributors and bus factor

A project that depends on one person is fragile. From a list of commit authors, find who did the most work and how many people the project depends on.

What to build

A program that ranks commit authors and finds the bus factor.

Requirements

  • Rank authors by number of commits, most first; ties are broken by name in alphabetical order. Print at most the top 3 as RANK. NAME: COUNT (PERCENT%).
  • PERCENT is the share of all commits, rounded to a whole number with halves rounded up.
  • Then print bus factor: K, where K is the smallest number of top authors whose commits add up to more than half of all commits.

Input

The first line has n (1 to 200). Each of the next n lines is the author of one commit; a name may contain spaces.

Output

Up to three ranked lines, then the bus factor line.

Constraints

1 ≤ n ≤ 200.

Example

Input:

6
Ada
Ada
Ada
Grace
Linus
Ada

Output:

1. Ada: 4 (67%)
2. Grace: 1 (17%)
3. Linus: 1 (17%)
bus factor: 1

Why it works

Sorting by count and then name gives a fixed ranking even with ties; the bus factor is a running total over the same sorted list, stopping as soon as it passes half of the commits.

Examples

Example 1 · Input
6
Ada
Ada
Ada
Grace
Linus
Ada
Expected output
1. Ada: 4 (67%)
2. Grace: 1 (17%)
3. Linus: 1 (17%)
bus factor: 1
Example 2 · Input
4
Ada
Grace
Linus
Ada
Expected output
1. Ada: 2 (50%)
2. Grace: 1 (25%)
3. Linus: 1 (25%)
bus factor: 2

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Count commits per author with a dictionary, then sort with key=lambda p: (-p[1], p[0]).

Hint 2

For the percentage use whole-number arithmetic: (200 * count + total) // (2 * total) rounds half up.

Hint 3

For the bus factor, add up the sorted counts one by one until the sum is more than half of the total.

Solution

Show one correct solution (try it yourself first)
n = int(input())
counts = {}
for _ in range(n):
    author = input().strip()
    counts[author] = counts.get(author, 0) + 1
ranked = sorted(counts.items(), key=lambda p: (-p[1], p[0]))
for rank, (name, count) in enumerate(ranked[:3], 1):
    percent = (200 * count + n) // (2 * n)
    print(f"{rank}. {name}: {count} ({percent}%)")
running = 0
factor = 0
for name, count in ranked:
    running += count
    factor += 1
    if running * 2 > n:
        break
print(f"bus factor: {factor}")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: What open source is →