easy +10 pts

Broadcast Add Scalar

Write a pure Python function to add a scalar to every element of a 2D list and return a new list.

Write a function `add_scalar_to_all(grid, scalar)` that takes a 2D list of numbers `grid` and a number `scalar`. The function must return a new 2D list where `scalar` has been added to every element of the original grid. The original `grid` must remain unchanged. You may use explicit loops or list comprehensions.

Constraints

- `grid` is a non-empty 2D list of integers or floats, with at least 1 row and 1 column. - All rows have the same length. - `scalar` is an integer or float. - The returned list must have the same dimensions as `grid`. - Do not modify the input list.

Example

['>>> add_scalar_to_all([[1, 2], [3, 4]], 10)', '[[11, 12], [13, 14]]', '>>> grid = [[1, 2], [3, 4]]', '>>> add_scalar_to_all(grid, 10)', '[[11, 12], [13, 14]]', '>>> grid', '[[1, 2], [3, 4]]']
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a nested loop or a list comprehension for each row.
To keep the original unchanged, create a new list of new lists.
Remember to add the scalar to each element inside each row.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.