Inspired by this
In the linked challenge, we are asked to apply addition to the elements of the original and the reverse of the input array. In this challenge, we are going to make it slightly more difficult, by introducing the other basic math operations.
Given an array of integers, cycle through +, *, -, //, %, ^
, where //
is integer division and ^
is exponent, while applying it to the reverse of the array. Or, in other words, apply one of the above functions to each element of an array, with the second argument being the reverse of the array, with the function applied cycling through the above list. This may still be confusing, so lets work through an example.
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Reverse: [9, 8, 7, 6, 5, 4, 3, 2, 1]
[ 1, 2, 3, 4, 5, 6, 7, 8, 9]
Operand: + * - / % ^ + * -
[ 9, 8, 7, 6, 5, 4, 3, 2, 1]
Result: [10, 16, -4, 0, 0, 1296, 10, 16, 8]
so the output for [1, 2, 3, 4, 5, 6, 7, 8, 9]
would be [10, 16, -4, 0, 0, 1296, 10, 16, 8]
To cover the corner cases, the input will never contain a 0, but may contain any other integer in the range from negative infinity to positive infinity. You may take input as a list of strings representing digits if you want.
Test cases
input => output
[1, 2, 3, 4, 5, 6, 7, 8, 9] => [10, 16, -4, 0, 0, 1296, 10, 16, 8]
[5, 3, 6, 1, 1] => [6, 3, 0, 0, 1]
[2, 1, 8] => [10, 1, 6]
[11, 4, -17, 15, 2, 361, 5, 28] => [39, 20, -378, 7, 2, 3.32948887119979e-44, 9, 308]
This is a code-golf so shortest code (in bytes) wins!