argsort tells you where each value of the sorted array came from. Compute np.argsort(a, kind="stable").
What to build
A program that prints the stable argsort of a list of numbers.
Requirements
- Print the positions that sort the values in ascending order, keeping equal values in their original order.
- Use NumPy's 1-D format: square brackets, numbers right-aligned to the widest one and separated by one space.
Input
One line of whole numbers.
Output
One line.
Constraints
1 to 1000 numbers; |x| ≤ 10⁹.
Example
Input:
30 10 20
Output:
[1 2 0]
Why it works
Sorting the positions by the values they point to gives exactly the order in which the values appear when sorted, and a stable sort keeps tied positions in increasing order.
Examples
Example 1 · Input
30 10 20
Expected output
[1 2 0]
Example 2 · Input
5 5 1
Expected output
[2 0 1]
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
sorted(range(n), key=lambda i: a[i]) gives the positions in sorted order; Python's sort is stable.
Hint 2
Print the positions in NumPy's 1-D format, right-aligned to the widest.
Solution
Show one correct solution (try it yourself first)
a = [int(x) for x in input().split()]
order = sorted(range(len(a)), key=lambda i: a[i])
w = max(len(str(i)) for i in order)
print("[" + " ".join(str(i).rjust(w) for i in order) + "]")