Given:
- a blackbox function \$f : \mathbb N \to \mathbb N\$,
- a list of positive integers \$L\$, and
- a list of indices \$I\$,
apply \$f(x)\$ to the elements of \$L\$ at the indices specified in \$I\$.
For example, \$f(x) = x^2, L = [7, 6, 3, 9, 1, 5, 2, 8, 4]\$ and \$I = [3, 5, 8]\$ (using 1 indexing), we apply \$f(x)\$ to the 3rd, 5th and 8th elements, yielding \$[7, 6, 9, 9, 1, 5, 2, 64, 4]\$
You may use 0 or 1 indexing for \$I\$. The elements of \$I\$ will be unique (there will be no duplicates), the maximum element of \$I\$ will never exceed the length of \$L\$ (or the length of \$L\$ minus 1, if using 0 indexing) and the minimum will never be below 1 (or 0, for 0 indexing).
Neither \$I\$ nor \$L\$ will ever be empty. \$f\$ will always return a positive integer, and will always take a single positive integer. \$L\$ may be in any order, however, you may decide if \$I\$ is in any specific order (e.g. sorted ascending).
This is code-golf, so the shortest code in bytes
Test cases
f(x)
L
I (using 1 indexing)
out
f(x) = x²
[14, 14, 5, 15, 15, 10, 13, 9, 3]
[1, 3, 4, 5, 6, 7]
[196, 14, 25, 225, 225, 100, 169, 9, 3]
f(x) = x+1
[7, 12, 14, 6]
[1, 2, 3, 4]
[8, 13, 15, 7]
f(x) = σ₀(x)+x
[13, 11, 9, 15, 16, 16, 16, 11, 6, 4]
[2, 4, 6, 8, 10]
[13, 13, 9, 19, 16, 21, 16, 13, 6, 7]
where \$\sigma_0(x)\$ is the number of divisors function.
This is a builtin in Jelly. This is a program where you can modify the function \$f\$ in the Header, and it will generate a random L
, I
then out
on separate lines. The final test case is prefilled into the Header.
¦
quick, which does exactly as the task asks for, usingÇ
(previous link) as \$f\$, and⁹
to indicate that we useI
as the indices link, to result inÇ⁹¦
\$\endgroup\$I
be aSet
object instead of aList
? \$\endgroup\$