18
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!
Sandbox (2k+ only) – caird coinheringaahing – 2017-09-11T19:29:22.677
@AdmBorkBork He is addressing it, I pointed that out in chat. – Mr. Xcoder – 2017-09-11T19:34:42.527
@AdmBorkBork corrected. I missed that in my test case generator – caird coinheringaahing – 2017-09-11T19:37:20.653
Your third test case still contains 0 >.> – Mr. Xcoder – 2017-09-11T19:43:26.243
I was wrong, Ignore that – Mr. Xcoder – 2017-09-11T19:49:22.380
In the last example we have 361<sup>-17</sup>. Are there any guidelines about the precision to which this should be calculated? – Digital Trauma – 2017-09-11T22:53:08.420
@DigitalTrauma your program has to handle standard precision rules. If you have to do extra to make it perfectly match, don't do the extra. – caird coinheringaahing – 2017-09-11T22:54:21.097
@cairdcoinheringaahing I'm using
bc
to do my arithmetic, which by default uses integer precision. Are you OK with an answer of 0 for the 361<sup>-17</sup> case, or should I "do the extra" to calculate to set bc to calculate exponents to 60 or so decimal places? – Digital Trauma – 2017-09-11T22:57:59.2431@DigitalTrauma for languages which default to integers, I think outputting 0 is acceptable for tiny numbers like that. – caird coinheringaahing – 2017-09-11T22:59:38.353
How flexible are you on not doing "the extra"? The exponentiation method in Japt can't (directly) take a negative number as an argument. I could work around that at a cost of 6 bytes (currently) or I could try using the exponentiation operator instead (but that would probably cost more bytes). Also, would Arnauld's original 60 byte solution (see his edit history) now be valid? – Shaggy – 2017-09-12T09:24:46.573
@Shaggy if the language's native number type can't handle floats without some extreme twisting of the arm (like bc), then you can not do the extra. Javascript, and by extension Japt, can handle floats quite easily, so no, it wouldn't be allowed. Sorry. – caird coinheringaahing – 2017-09-12T14:12:31.610
Though that might be the case; thanks for clarifying. – Shaggy – 2017-09-12T14:13:16.453
@cairdcoinheringaahing:
bc
does fixed-point decimal (default is zero places after the decimal, invoke asbc -l
to set 20 places after the decimal and include math libraries), but it doesn't do scientific notation or floating point. So with only 3 more bytes ofbash
script, you could get stuff like361 ^ -2
to print as.00000767336039471765
instead of0
. But then you'd have to mess withscale
to get integer division instead of fixed-point... So yeah, I guess it's maybe justified to give bogus answers even for small negative exponents inbc
, since it doesn't have float or types – Peter Cordes – 2017-09-17T03:35:23.740