NumPy: Unique values and counts

np.unique(a, return_counts=True) is the quickest frequency table in NumPy. Model it.

What to build

A program that prints the sorted distinct values and how often each appears.

Requirements

  • Print values followed by the sorted distinct values, then counts followed by the count of each, in the same order.
  • Format each list in NumPy's 1-D format (brackets, right-aligned to the widest number in that list, single spaces).

Input

One line of whole numbers.

Output

Two lines.

Constraints

1 to 1000 numbers; |x| ≤ 10⁹.

Example

Input:

3 1 2 3 3 1

Output:

values [1 2 3]
counts [2 1 3]

Why it works

Sorting the distinct values gives np.unique's order, and counting each value in the original data gives the matching counts.

Examples

Example 1 · Input
3 1 2 3 3 1
Expected output
values [1 2 3]
counts [2 1 3]
Example 2 · Input
7
Expected output
values [7]
counts [1]

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

Hints

Hint 1

sorted(set(a)) gives the distinct values in order.

Hint 2

Each of the two lines is right-aligned separately, to its own widest number.

Solution

Show one correct solution (try it yourself first)
def fmt(values):
    w = max(len(str(v)) for v in values)
    return "[" + " ".join(str(v).rjust(w) for v in values) + "]"


a = [int(x) for x in input().split()]
values = sorted(set(a))
counts = {}
for x in a:
    counts[x] = counts.get(x, 0) + 1
print("values", fmt(values))
print("counts", fmt([counts[v] for v in values]))

Lesson: Sorting, unique values and searching →