Write a map routine from scratch. Given an integer array arr and a mapping function fn, return a new array result such that result[i] = fn(arr[i], i) for each index i. The mapping function takes two arguments: the current element and its index.
Do not call a built-in map (Python's map or JavaScript's Array.prototype.map). Iterate over the array yourself and build the result array.
So the function can be tested with plain inputs, the mapping function is fixed as fn(x, i) = x * multiplier + i, and the integer multiplier is passed in along with the array.
Function Signature
def custom_map(arr: list[int], multiplier: int) -> list[int]:
Rules
-
i
is the 0-based index of the element, so the first element gets
+ 0
.
-
Return a new list with the same length and order as
arr
. Do not modify
arr
.
Constraints
-
1 <= len(arr) <= 1000
-
-10^9 <= arr[i] <= 10^9
-
multiplier
is an integer.
Examples
Input: arr = [1,2,3,4], multiplier = 2
Output: [2,5,8,11]
With fn(x, i) = x*2 + i, the values are 1*2+0 = 2, 2*2+1 = 5, 3*2+2 = 8 and 4*2+3 = 11.
Input: arr = [5,-1,0,7,3], multiplier = 3
Output: [15,-2,2,24,13]
With fn(x, i) = x*3 + i, the values are 15+0, -3+1, 0+2, 21+3 and 9+4.