📘
Basic Array Arithmetic
Problem Statement
Perform basic arithmetic operations on NumPy arrays.
Background
NumPy supports element-wise arithmetic:
- arr + n: Add n to each element
- arr - n: Subtract n from each element
- arr * n: Multiply each element by n
- arr / n: Divide each element by n
Your Task
Write a function array_arithmetic(arr, n) that returns a dictionary with:
- "add": Array after adding n (as list)
- "subtract": Array after subtracting n (as list)
- "multiply": Array after multiplying by n (as list)
- "divide": Array after dividing by n (as list, rounded to 2 decimals)
Output Format
Return a dictionary with exactly these four keys.
Example:
Input:
arr = [10, 20, 30], n = 5
Output:
{'add': [15, 25, 35], 'subtract': [5, 15, 25], 'multiply': [50, 100, 150], 'divide': [2.0, 4.0, 6.0]}Reasoning:
Element-wise operations apply to each element
Constraints:
- n will never be 0
- Round division results to 2 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.