easy +7 pts

Pad array edges

Pad a list on both ends with a specified value to a target length.

Write a function `pad_edges(nums, length, pad=0)` that returns a new list of length `length` by adding padding value `pad` to the beginning and end of the input list `nums`. The padding is applied so that the original elements are kept in the center: if there are extra positions, the front padding count is computed as `total // 2` rounded up (ceiling) and the back gets the rest. For example, with 3 extra positions, 2 go to the front and 1 to the back. If `length` is less than or equal to the length of `nums`, return a copy of `nums` without truncation. The original list should not be modified. **Function signature:** `def pad_edges(nums: list[int], length: int, pad: int = 0) -> list[int]:` **Examples:** ``` >>> pad_edges([1,2,3], 5) [0, 1, 2, 3, 0] >>> pad_edges([1,2,3], 7, pad=-1) [-1, -1, 1, 2, 3, -1, -1] >>> pad_edges([1,2,3], 3, pad=9) [1, 2, 3] >>> pad_edges([5], 4, pad=2) [2, 2, 5, 2] >>> pad_edges([], 4, pad=7) [7, 7, 7, 7] ```

Constraints

- `0 <= len(nums) <= 1000` - `0 <= length <= 2000` - `pad` is any integer - The returned list length is `max(length, len(nums))`. - Do not modify the input list.

Example

>>> pad_edges([1,2,3], 5)
[0, 1, 2, 3, 0]
>>> pad_edges([1,2,3], 7, pad=-1)
[-1, -1, 1, 2, 3, -1, -1]
>>> pad_edges([1,2,3], 3, pad=9)
[1, 2, 3]
>>> pad_edges([5], 4, pad=2)
[2, 2, 5, 2]
>>> pad_edges([], 4, pad=7)
[7, 7, 7, 7]
7 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Calculate total padding needed: `total = length - len(nums)` (if positive).
Decide how many padding to put in front: `front = (total + 1) // 2` (ceiling).
The back gets the remaining padding: `back = total - front`.
Concatenate: `[pad] * front + nums + [pad] * back`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.