Apply a chain of scalar operations to an integer array, as NumPy does in one step each.
What to build
A program that applies operations to every element and prints the array after each.
Requirements
- Each operation is
+ K,- K,* K,// K,% Kor** K. Apply it to every element and print the array in NumPy's 1-D format (brackets, numbers right-aligned to the widest, single spaces).
Input
Line 1: the array's whole numbers. Line 2: n. Then n operations.
Output
n lines.
Constraints
1 to 30 numbers; 1 ≤ n ≤ 20; K ≠ 0 for // and %; 0 ≤ K ≤ 4 for **; values stay below 10^12.
Example
Input:
1 2 3 4
3
* 3
+ 1
// 2
Output:
[ 3 6 9 12]
[ 4 7 10 13]
[2 3 5 6]
Why it works
A scalar combines with every element, so each operation is one whole-array step, and integer // and % follow the same rules as Python's.
Examples
1 2 3 4 3 * 3 + 1 // 2
[ 3 6 9 12] [ 4 7 10 13] [2 3 5 6]
-7 7 2 // 2 % 3
[-4 3] [2 0]
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Apply each operation to every element, keeping the result for the next operation.
Hint 2
Python's // and % on negative numbers round toward minus infinity, exactly as NumPy does for integers.
Solution
Show one correct solution (try it yourself first)
OPS = {"+": lambda x, k: x + k, "-": lambda x, k: x - k, "*": lambda x, k: x * k, "//": lambda x, k: x // k, "%": lambda x, k: x % k, "**": lambda x, k: x**k}
a = [int(x) for x in input().split()]
n = int(input())
for _ in range(n):
op, k = input().split()
a = [OPS[op](x, int(k)) for x in a]
w = max(len(str(v)) for v in a)
print("[" + " ".join(str(v).rjust(w) for v in a) + "]")