Loading...
「ツール」は右上に移動しました。
利用したサーバー: wtserver1
1いいね 24 views回再生

One-Liner NumPy Ops: Sum, Subtract, Multiply, Floor Div, Mod, Power #Python #NumPy

This solution demonstrates a compact one-liner approach to perform element-wise operations on two 2-D arrays using Python’s NumPy module. In this one-liner version, the code:

Reads Input (#InputHandling):
It first reads the dimensions N and M, then reads the arrays A and B from the input.

Converts Input to NumPy Arrays (#NumPy):
Using list comprehensions and the np.array() function, the input lists are converted into NumPy arrays for efficient computation.

Performs Arithmetic Operations (#ArithmeticOps):
It performs addition, subtraction, multiplication, integer division (using //), modulus, and power operations in a vectorized manner.

Outputs the Results (#OutputFormatting):
Each result is printed on a separate line, preserving the required order.

This compact, one-liner approach is ideal for competitive programming and interviews, where brevity and efficiency are essential. It showcases the power of NumPy's vectorized operations and Python’s ability to condense multiple steps into a single line of code.

Code (One-Liner Version):

import numpy as np
def solve():
n, m = map(int, input().split());
A = np.array([list(map(int, input().split())) for _ in range(n)]);
B = np.array([list(map(int, input().split())) for _ in range(n)]);
print(A+B); print(A-B); print(A*B); print(A//B); print(A%B); print(A**B)
if _name_ == '__main__':
solve()

コメント