fill()
Fills elements of array with value from start up to, but not including, end.
Implementation
Args: array: List to fill. value: Value to fill with. start: Index to start filling. Defaults to ``0``. end: Index to end filling. Defaults to ``len(array)``. Returns: Filled array.
Example
fill([1, 2, 3, 4, 5], 0)
Expected output: [0, 0, 0, 0, 0]
Alternative usage:
fill([1, 2, 3, 4, 5], 0, 1, 3)
Expected output: [1, 0, 0, 4, 5]
Source Code
def fill(
array: List[T], value: T, start: int = 0, end: Optional[int] = None
) -> List[T]:
length = len(array)
if length == 0:
return array
s = start if start >= 0 else max(length + start, 0)
e = end if end is not None else length
e = e if e >= 0 else max(length + e, 0)
s = min(max(s, 0), length)
e = min(max(e, 0), length)
for i in builtins.range(s, e):
array[i] = value
return array