| Bytes | Lang | Time | Link |
|---|---|---|---|
| 065 | Juby | 250513T211308Z | Jordan |
| 069 | Factor + sequences.repeating | 220507T043320Z | chunes |
| 104 | Desmos | 220507T040839Z | Aiden Ch |
| 042 | J | 170911T220458Z | cole |
| 015 | Jelly | 170911T212317Z | Jonathan |
| 332 | Java 8 | 171011T142608Z | Kevin Cr |
| 108 | CasioBasic | 171010T083904Z | numberma |
| 2723 | MATL | 170912T073834Z | Cinaski |
| 040 | k | 170912T200628Z | zgrep |
| 105 | Haskell | 170911T202354Z | jferard |
| 067 | Python 2 | 170911T200514Z | totallyh |
| 057 | Ruby | 170912T094443Z | G B |
| 6766 | Perl 6 | 170911T201321Z | Ramillie |
| 074 | R | 170911T234712Z | Giuseppe |
| nan | Perl 5 | 170911T215950Z | Xcali |
| 016 | Husk | 170911T193449Z | H.PWiz |
| 053 | Bash + GNU utilities | 170911T212802Z | Digital |
| 018 | 05AB1E | 170911T210059Z | Emigna |
| 071 | Python 2 | 170911T193056Z | Mr. Xcod |
| 067 | JavaScript ES7 | 170911T193836Z | Arnauld |
| 010 | Jelly | 170911T195159Z | miles |
J-uby, 65 bytes
Between the :<<&:<< and the double :zip this is some pretty silly J-uby.
:zip%[:+@|:take&%i[+ * - / % ^ + * -].cycle,:zip%:~]|:*&(:<<&:<<)
Explanation
Not an explanation per se, but this is the equivalent Ruby. Yep, it would be simpler to skip the nested zip and put the receiver/arguments in a different order, but it would cost a lot of bytes in the conversion to J-uby.
->a{
%i[+ * - / % ^ + * -].cycle.take(a.size)
.zip(a.zip(a.reverse)).map {|o,(x,y)| o.(x, y) }
}
Factor + sequences.repeating, 69 bytes
[ dup reverse { + * - /i mod ^ } over length cycle [ execute ] 3map ]
How?
! { 1 2 3 4 5 6 7 8 9 }
dup ! { 1 2 3 4 5 6 7 8 9 } { 1 2 3 4 5 6 7 8 9 }
reverse ! { 1 2 3 4 5 6 7 8 9 } { 9 8 7 6 5 4 3 2 1 }
{ + * - /i mod ^ } ! { 1 2 3 4 5 6 7 8 9 } { 9 8 7 6 5 4 3 2 1 } { + * - /i mod ^ }
over ! { 1 2 3 4 5 6 7 8 9 } { 9 8 7 6 5 4 3 2 1 } { + * - /i mod ^ } { 9 8 7 6 5 4 3 2 1 }
length ! { 1 2 3 4 5 6 7 8 9 } { 9 8 7 6 5 4 3 2 1 } { + * - /i mod ^ } 9
cycle ! { 1 2 3 4 5 6 7 8 9 } { 9 8 7 6 5 4 3 2 1 } { + * - /i mod ^ + * - }
[ execute ] 3map ! { 10 16 -4 0 0 1296 10 16 8 }
Desmos, 107 104 bytes
k=l.length
L=mod([1...k],6)
R=l[k...1]
f(l)=\{L=1:l+R,L=2:lR,L=3:l-R,L=4:\floor(l/R),L=5:\mod(l,L),l^R\}
Very straightforward implementation of the challenge, can probably be golfed further.
J, 44 42 bytes
Crossed out 44, yada yada...
-2 bytes thanks to @ConorO'Brien
_2+/`(*/)`(-/)`(<.@%/)`(|~/)`(^/)\[:,],.|.
So many parens and inserts... Surely there's a better way to do this (maybe using insert rather than infix?)
Explanation
_2(+/)`(*/)`(-/)`(<.@%/)`(|~/)`(^/)\[:,],.|. Input: a
],.|. Join a with reverse(a)
, Ravel (zip a with reverse(a))
_2 \ To non-overlapping intervals of 2
(+/)`(*/)`(-/)`(<.@%/)`(|~/)`(^/) Apply the cyclic gerund
+/ Insert addition
*/ Insert multiplication
-/ Insert subtraction
<.@%/ Insert integer division
|~/ Insert mod
^/ Insert exponentiation
Some notes:
J doesn't have integer division, so we compose %-division with >.-floor. J's mod (|) does the reverse order of what we'd expect, so we have to invert its order using ~-reflexive.
Even though we're moving over intervals of 2, we have to use /-insert to insert the verbs to have them be used dyadically since that's how \-infix works.
Jelly, 15 bytes
żṚj"“+×_:%*”ṁ$V
Try it online! or see the test-suite.
How?
żṚj"“+×_:%*”ṁ$V - Link: list of numbers, a e.g. [5, 3, 6, 1, 1]
Ṛ - reverse a [1, 1, 6, 3, 5]
ż - interleave [[5,1],[3,1],[6,6],[1,3],[1,5]]
$ - last two links as a monad:
“+×_:%*” - literal list of characters ['+','×','_',':','%','*']
ṁ - mould like a ['+','×','_',':','%']
" - zip with the dyad:
j - join ["5+1","3×1","6_6","1:3","1%5"]
V - evaluate as Jelly code (vectorises) [6, 3, 0, 0, 1]
Java 8, 336 332 bytes
import java.math.*;a->{int b[]=a.clone(),l=b.length,i=l/2,s,t;for(;i-->0;b[i]=b[l+~i],b[l+~i]=t)t=b[i];BigInteger r[]=new BigInteger[l],u,v;for(;++i<l;t=b[i],v=new BigInteger(t+""),r[i]=(s=i%6)<1?u.add(v):s<2?u.multiply(v):s<3?u.subtract(v):s<4?u.divide(v):s<5?u.remainder(v):t<0?u.ZERO:u.pow(t))u=new BigInteger(a[i]+"");return r;}
-4 bytes thanks to @ceilingcat.
Sigh..
Input as int[], output as java.math.BigInteger[].
Without the rule "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.", using integers in the range -2147483648 to 2147483647, it would be 186 182 bytes instead (input as int[], and no output because it modifies this input-array instead to save bytes):
a->{int b[]=a.clone(),l=b.length,i=l/2,t,u,v;for(;i-->0;b[i]=b[l+~i],b[l+~i]=t)t=b[i];for(;++i<l;v=b[i],a[i]=(t=i%6)<1?u+v:t<2?u*v:t<3?u-v:t<4?u/v:t<5?u%v:(int)Math.pow(u,v))u=a[i];}
Explanation:
import java.math.*; // Required import for BigInteger
a->{ // Method with int[] parameter and BigInteger[] return-type
int b[]=a.clone(), // Make a copy of the input-array
l=b.length, // Length of the input
i=l/2, // Index-integer, starting at halve the length
s,t; // Temp integers
for(;i-->0;b[i]=b[l+~i],b[l+~i]=t)t=b[i];
// Reverse the input-array and store it in `b`
BigInteger r[]=new BigInteger[l],
// Result-array
u,v; // Temp BigIntegers
for(;++i<l; // Loop over the array(s):
// After every iteration:
t=b[i], // Set the current item of `b` in `t`
v=new BigInteger(t+""), // And also set it in `v` as BigInteger
r[i]=(s=i%6)<1? // If the index `i` modulo-6 is 0:
u.add(v) // Add the items with each other
:s<2? // Else-if index `i` modulo-6 is 1:
u.multiply(v) // Multiply the items with each other
:s<3? // Else-if index `i` modulo-6 is 2:
u.subtract(v) // Subtract the items with each other
:s<4? // Else-if index `i` modulo-6 is 3:
u.divide(v) // Divide the items with each other
:s<5? // Else-if index `i` modulo-6 is 4:
u.remainder(v) // Use modulo for the items
: // Else (index `i` modulo-6 is 5):
t<0? // If `t` is negative:
u.ZERO // Simply use 0
: // Else:
u.pow(t)) // Use the power of the items
u=new BigInteger(a[i]+""); // Set the current item of `a` to `u` as BigInteger
return r;} // After the loop, return the result BigInteger-array
Casio-Basic, 108 bytes
{x+y,x*y,x-y,int(x/y),x-int(x/y)y,x^y}⇒o
dim(l)⇒e
Print seq(o[i-int(i/6)6+1]|{x=l[i+1],y=l[e-i]},i,0,e-1)
That was painful. Especially because mod(x,y) returns x when it really shouldn't, which meant I had to make my own mod function: hence the x-int(x/y)y.
Loops i from 0 to length(l)-1, taking successive elements in the o list and applying l[i] for x and l[-i] for y. (negative indices don't work though, so instead I subtract i from the length of the list and take that index.)
107 bytes for the function, +1 byte to add l in the parameters box.
MATL, 27 23 bytes
-4 bytes thanks to @LuisMendo
tP+1M*1M-IM&\w1M^v"@X@)
Explanation:
tP % duplicate and flip elements
+ % push array of sums (element-wise)
1M* % push array of products (element-wise)
1M- % push array of subtractions (element-wise)
IM&\w % push array of divisions and modulo (element-wise)
1M^ % push array of power (element-wise)
v % vertically concatenate all arrays
"@X@) % push to stack values with the correct index based on operator
% (implicit) convert to string and display
k, 40 bytes
{_((#x)#(+;*;-;%;{y!x};{*/y#x})).'x,'|x}
{ } /function(x)
|x /reverse x
x,' /zip concat with x
( ; ; ; ; ; ) /list of operations
+ * - % /add, mult, sub, div
{y!x} /mod (arguments need to be reversed)
{*/y#x} /pow (repeat and fold multiply)
((#x)# ) /resize operations to length of x
.' /zip apply
_ /floor result
Haskell, 74 117 105 bytes
x#y=fromIntegral.floor$x/y
x%y=x-x#y
f u=[o a b|(o,a,b)<-zip3(cycle[(+),(*),(-),(#),(%),(**)])u(reverse u)]
Saved 12 bytes thanks to @nimi
There is certainly a better way to achieve this.
EDIT 1. Fixed exponent for integers; 2. There's definitely a better way, see comment below: 95 91 bytes
x#y=fromIntegral.floor$x/y
x%y=x-x#y
f=zipWith3($)(cycle[(+),(*),(-),(#),(%),(**)])<*>reverse
Python 2, 67 bytes
-3 bytes thanks to ovs.
lambda l:[eval(j+'*+*-/%*'[-~i%6::6]+l[~i])for i,j in enumerate(l)]
Python 2, 95 bytes
lambda l:[[add,mul,sub,div,mod,pow][i%6](v,l[~i])for i,v in enumerate(l)]
from operator import*
eval is evil... but perhaps more golfy. :P
Ruby, 63 57 bytes
->a{t=0;a.map{|x|eval [x,a[t-=1]]*%w(** % / - * +)[t%6]}}
Nothing fancy, really. Just iterate on the array, use an index as reverse iterator, join into a string using the right operator, evaluate, rinse and repeat.
Perl 6, 67 66 bytes
Saved 1 byte thanks to @nwellnhof.
{map {EVAL ".[0] {<+ * - div % **>[$++%6]} .[1]"},zip $_,.reverse}
Very unimaginative (and probably bad) solution. Zips the argument with itself reversed. The resulting list is then mapped with the block that EVALs the string a (operator) b. The operator is chosen from the list of strings <+ * - div % **> using the free state (think static in C — the value persists across the calls of the block) variable $. This is created for each block separately and set to 0. You can do anything you like with it, but you may reference it only once (each occurence of $ refers to another variable, actually). So $++%6 is actually 0 during the first call, 1 during the second, ... 5 during the 6th, 0 during the 7th and so on.
I at first tried to do without an EVAL. The operators are in fact just subs (= functions), but their names are so extremely ungolfy (&infix:<+> and so on) that I had to forgo that approach.
R, 74 bytes
function(l)Map(Map,c(`+`, `*`, `-`, `%/%`, `%%`,`^`),l,rev(l))[1:sum(l|1)]
This is the final answer I came up with. It returns a list of length length(l) where each element is a list containing the corresponding element. Kinda crappy but they're all there. If that's unacceptable, either of the Map can be replaced with mapply for +3 bytes.
Since R operators are all functions (the infix notation just being syntactic sugar), I tried to select one from a list; for example, the 94 byte solution below.
To try and get rid of the loop, I tried sapply, but that only works with a single function and input list. Then I remembered the multivariate form, mapply, which takes an n-ary function FUN and n succeeding arguments, applying FUN to the first, second, ..., elements of each of the arguments, recycling if necessary. There is also a wrapper function to mapply, Map that "makes no attempt to simplify the result". Since it's three bytes shorter, it's a good golfing opportunity.
So I defined a trinary function (as in the 80 byte solution below) that takes a function as its first argument, and applies it to its second and third ones. However, I realized that Map is a function that takes a function as its first argument and applies it to successive ones. Neat!
Finally, we subset at the end to ensure we only return the first length(l) values.
R, 80 bytes
function(l)Map(function(x,y,z)x(y,z),c(`+`, `*`, `-`, `%/%`, `%%`,`^`),l,rev(l))
This one doesn't work, as it will return 6 values for lists with less than 6 elements.
R, 94 bytes
function(l){for(i in 1:sum(l|1))T[i]=switch(i%%6+1,`^`,`+`,`*`,`-`,`%/%`,`%%`)(l,rev(l))[i]
T}
Explanation (mildly ungolfed):
function(l){
for(i in 1:length(l)){
fun <- switch(i%%6+1,`^`,`+`,`*`,`-`,`%/%`,`%%`) # select a function
res <- fun(l,rev(l)) # apply to l and its reverse
T[i] <- res[i] # get the i'th thing in the result
}
T # return the values
}
Because each of the functions is vectorized, we can index at the end (res[i]). This is better than the eval approach below.
R, 100 bytes
function(l)eval(parse(t=paste("c(",paste(l,c("+","*","-","%/%","%%","^"),rev(l),collapse=","),")")))
This is the shortest eval approach I could find; because we have to collect the results into one vector, we need to paste a c( ) around all the expressions, which adds a ton of unnecessary bytes
Perl 5, 68 + 1 (-p) = 69 bytes
print$",eval'int 'x($i%6==3).$_.qw|+ ** % / - *|[$i--%6].$F[$i]for@F
Takes input as space separated list of numbers.
Husk, 16 bytes
This challenge favours languages that can create infinite lists of functions. Maybe not, eval FTW
zF¢+ë+*-÷e%^Ṡze↔
How?
¢+ë+*-÷e%^ The infinite list [+,*,-,÷,%,^,+,*,-,...
ë+*-÷ The list [+,*,-,÷]
e%^ The list [%,^]
+ Concatenated
¢ Then repeated infinitely
↔ The input reversed e.g [9,8,7,6,5,4,3,2,1]
Ṡze Zipped with itself [[9,1],[8,2],[7,3],[6,4],[5,5],[4,6],[3,7],[2,8],[1,9]]
zF Zipwith reduce, the list of functions and the list of lists.
[F+[9,1],F*[8,2],F-[7,3],F÷[6,4],F%[5,5],F^[4,6],F+[3,7],F*[2,8],F-[1,9]]
[10 ,16 ,-4 ,0 ,0 ,1296 ,10 ,16 ,8 ]
Alternative 17 byte solution:
ṠozIzI¢+ë+*-÷e%^↔
Bash + GNU utilities, 53
tac $1|paste -d, $1 -|tr ',
' '
;'|paste -sd+*-/%^|bc
This script takes a filename as a command-line parameter.
The nice thing here is that paste -d allows a list of separators to be given, which are used cyclically. The rest it just getting the input into the right format to do this.
05AB1E, 18 bytes
Â"+*-÷%m"Ig×)øε`.V
Explanation
 # push a reversed copy of the input
"+*-÷%m" # push the list of operators
Ig× # repeat it input times
)ø # zip together
ε # apply to each triplet
` # push separately to stack
.V # evaluate
Python 2, 71 bytes
lambda l:[eval(y+'+*-/%*'[x%6]*-~(x%6>4)+l[~x])for x,y in enumerate(l)]
Saved 2 bytes thanks to ovs!
JavaScript (ES7), 68 67 bytes
a=>[...a].map((v,i)=>(x=a.pop(),o='+*-/%'[i%6])?eval(v+o+x)|0:v**x)
let f =
a=>[...a].map((v,i)=>(x=a.pop(),o='+*-/%'[i%6])?eval(v+o+x)|0:v**x)
console.log(JSON.stringify(f([1, 2, 3, 4, 5, 6, 7, 8, 9] ))) // [10, 16, -4, 0, 0, 1296, 10, 16, 8]
console.log(JSON.stringify(f([5, 3, 6, 1, 1] ))) // [6, 3, 0, 0, 1]
console.log(JSON.stringify(f([2, 1, 8] ))) // [10, 1, 6]
console.log(JSON.stringify(f([11, 4, -17, 15, 2, 361, 5, 28]))) // [39, 20, -378, 7, 2, 3.32948887119979e-44, 9, 308]
Jelly, 10 bytes (fork)
+×_:%*6ƭ"Ṛ
I was just working on implementing a quick for this the other day, so it's quite surprising to see a use for it so soon. It still only exists as a fork, so you cannot try it online.
Sample output
$ ./jelly eun '+×_:%*6ƭ"Ṛ' '[1,2,3,4,5,6,7,8,9]'
[10, 16, -4, 0, 0, 1296, 10, 16, 8]
$ ./jelly eun '+×_:%*6ƭ"Ṛ' '[5,3,6,1,1]'
[6, 3, 0, 0, 1]
$ ./jelly eun '+×_:%*6ƭ"Ṛ' '[2,1,8]'
[10, 1, 6]
$ ./jelly eun '+×_:%*6ƭ"Ṛ' '[11,4,-17,15,2,361,5,28]'
[39, 20, -378, 7, 2, 3.32948887119979e-44, 9, 308]
Explanation
+×_:%*6ƭ"Ṛ Input: array
6ƭ Tie 6 dyads
+ Addition
× Multiplication
_ Subtraction
: Integer division
% Modulo
* Power
" Vectorize with
Ṛ Reverse