NumPy: Add two arrays element-wise

Model the most basic vectorised operation, a + b, and print the result the way NumPy prints a 1-D array.

What to build

A program that adds two integer arrays element by element.

Requirements

  • If the two arrays have the same length, print their element-wise sum the way NumPy prints an integer array: in square brackets, numbers separated by one space, each right-aligned to the width of the widest number (a minus sign counts).
  • Otherwise print ValueError: operands could not be broadcast together with shapes (N,) (M,).

Input

Line 1: the first array's whole numbers, separated by spaces. Line 2: the second array's.

Output

One line.

Constraints

1 to 50 numbers per line; |x| ≤ 10^6.

Example

Input:

1 2 3
10 20 30

Output:

[11 22 33]

Why it works

NumPy adds matching elements in one compiled loop, and refuses arrays whose shapes cannot be lined up; its printing pads every number to the same width so that columns line up.

Examples

Example 1 · Input
1 2 3
10 20 30
Expected output
[11 22 33]
Example 2 · Input
1 2
1 2 3
Expected output
ValueError: operands could not be broadcast together with shapes (2,) (3,)

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

Hints

Hint 1

Check the lengths first; NumPy refuses arrays of different lengths here.

Hint 2

The width of each printed number is the length of the longest one.

Solution

Show one correct solution (try it yourself first)
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
if len(a) != len(b):
    print(f"ValueError: operands could not be broadcast together with shapes ({len(a)},) ({len(b)},)")
else:
    total = [x + y for x, y in zip(a, b)]
    width = max(len(str(v)) for v in total)
    print("[" + " ".join(str(v).rjust(width) for v in total) + "]")

Lesson: Why NumPy →