| Bytes | Lang | Time | Link |
|---|---|---|---|
| 034 | Swift 6 | 250429T181221Z | macOSist |
| 019 | Raku Perl 6 rakudo | 250423T154817Z | xrs |
| 018 | TIBASIC TI83 | 250331T161843Z | absolute |
| 048 | Tcl | 170725T115649Z | sergiol |
| 003 | Uiua | 231013T175118Z | chunes |
| 002 | Thunno 2 | 230611T135533Z | The Thon |
| 014 | Juby | 221120T204436Z | Jordan |
| 006 | K ngn/k | 220507T044540Z | oeuf |
| 022 | Desmos | 220507T034621Z | Aiden Ch |
| 007 | Risky | 220129T064419Z | Dyad Han |
| 4439 | tinylisp | 220202T194922Z | chunes |
| 002 | Vyxal | 220112T093905Z | chunes |
| 019 | HP 48 User REPL | 220104T014002Z | TheSlive |
| 008 | Add++ | 220102T002441Z | lyxal |
| 021 | Juby | 220102T063534Z | Razetime |
| 004 | Pip p | 220102T040912Z | DLosc |
| 018 | Factor | 211231T054844Z | chunes |
| 005 | K4 / K oK | 180503T195639Z | mkst |
| 005 | Stax | 180503T160107Z | wastl |
| 025 | CasioBasic | 171010T074237Z | numberma |
| 028 | ARBLE | 171010T223038Z | ATaco |
| 022 | 8th | 170821T170406Z | Chaos Ma |
| 005 | MY | 170726T182003Z | Adalynn |
| 053 | Java 8 | 170816T074218Z | Kevin Cr |
| 057 | C# .NET Core | 170724T184001Z | jkelm |
| nan | Perl 5 | 170816T031811Z | Xcali |
| 004 | Husk | 170815T225312Z | ბიმო |
| 057 | C++14 | 170806T110501Z | Karl Nap |
| 017 | Clojure | 170725T104053Z | cliffroo |
| 030 | Swift 3 | 170726T164957Z | Alexande |
| 058 | Swift | 170724T183257Z | Mr. Xcod |
| 021 | Clojure | 170726T061950Z | dmh |
| 019 | R | 170726T052429Z | Xingzhou |
| 027 | JavaScript ES6 | 170724T190347Z | Neil |
| 049 | Scala | 170725T144811Z | Tamoghna |
| 016 | R | 170724T182338Z | Giuseppe |
| 032 | PowerShell | 170724T182013Z | AdmBorkB |
| 014 | Pari/GP | 170725T124820Z | alephalp |
| 060 | C# .NET Core | 170724T213127Z | Grzegorz |
| 033 | Common Lisp | 170725T112726Z | user6516 |
| 019 | x86 Machine Code 32bit protected mode | 170725T095555Z | Cody Gra |
| 059 | PHP | 170725T005518Z | Titus |
| 002 | Jelly | 170724T181634Z | hyperneu |
| 003 | MATL | 170724T221323Z | DJMcMayh |
| 005 | J | 170724T214556Z | Richard |
| 033 | JavaScript ES6 | 170724T182156Z | Rick Hit |
| 032 | Python 2 | 170724T201307Z | ovs |
| 012 | Mathematica | 170724T183337Z | ZaMoC |
| 031 | Retina | 170724T200010Z | Neil |
| 022 | Röda | 170724T185936Z | user4180 |
| 049 | C | 170724T193059Z | orlp |
| 007 | Stacked | 170724T190756Z | Conor O& |
| 032 | Python 2 | 170724T181802Z | vaultah |
| 002 | Neim | 170724T190100Z | Okx |
| 003 | J | 170724T185740Z | Jonah |
| 030 | Racket | 170724T185040Z | Business |
| 031 | Python 2 + numpy | 170724T184625Z | wnnmaw |
| 026 | PowerShell | 170724T184806Z | Joey |
| 025 | Ruby | 170724T184117Z | Ventero |
| 007 | Japt | 170724T183255Z | Justin M |
| 003 | APL Dyalog | 170724T182634Z | user4180 |
| 003 | anyfix | 170724T183149Z | hyperneu |
| 004 | Actually | 170724T183139Z | Erik the |
| 015 | Perl 6 | 170724T183106Z | Sean |
| 031 | Ruby 2.4 | 170724T182812Z | Value In |
| 006 | Brachylog | 170724T182558Z | Erik the |
| 059 | Positron | 170724T182435Z | hyperneu |
| 040 | Python 2 | 170724T181725Z | totallyh |
| 007 | CJam | 170724T182254Z | Erik the |
| 003 | Ohm | 170724T182213Z | totallyh |
| 020 | Haskell | 170724T181902Z | Wheat Wi |
| 005 | Pyth | 170724T181919Z | Mr. Xcod |
| 003 | Pyth | 170724T181851Z | Erik the |
| 002 | 05AB1E | 170724T181716Z | Erik the |
TI-BASIC (TI-83), 18 bytes
Ans+seq(Ans(X),X,dim(Ans),1,⁻1
A simple program that adds the list in Ans with a sequence that reverses it.
The language does not support empty lists, but the other test cases are successful.
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
Program size is equal to \$MEM\: byte\: count - program\: name\: length - 9\: bytes\$.
J-uby, 14 bytes
:zip%:~|:*&+:+
:zip % :~ | # Zip array with its reverse, then
:* & +:+ # Map with sum
Risky, 7 bytes
0__2-?_+_?+_0
Appends -? (first input reversed) to __ (all inputs, which is in this case a trick to wrap the input array), and then transposes. Maps with sum, which is the most complicated usage of Risky's quicks that I'm even going to attempt.
tinylisp, 44 39 bytes
(load library
(q((p)(map* a p(reverse p
-5 bytes thanks to @DLosc
Explanation
An anonymous function that maps the a function (addition) over pairwise elements in the input and the reverse of the input, forming a new list.
HP 48 User REPL, 19 bytes
« DUP REVLIST +
»
Explanation
« and » mark the object as a program.
DUP - Duplicates the list
REVLIST - Reverses the list
+ - Sums them
Add++, 8 bytes
L,dbRz£+
Finally something sane with Add++
Explained
L,dbRz£+
L, ; create a lambda that:
dbR ; pushes the input list and its reverse
z ; zips those together
£+ ; and reduces each pair by addition
Pip -p, 4 bytes
g+Rg
Explanation
Just what it says on the tin.
g List of command-line arguments
R Reversed
g+ Add to the original list
(Addition operates element-wise on lists in Pip.)
Factor, 18 bytes
[ dup reverse v+ ]
Explanation
! { 1 2 3 }
dup ! { 1 2 3 } { 1 2 3 }
reverse ! { 1 2 3 } { 3 2 1 }
v+ ! { 4 4 4 }
K4 / K (oK), 5 bytes
Solution:
x+|x:
Example:
x+|x:,()
,()
x+|x:8 92
100 100
x+|x:,999
,1998
Explanation:
Not quite as elegant as the J solution, but same kinda thing:
x+|x: / the solution
x: / store input as x
| / reverse it
+ / add to
x / x
Stax, 5 bytes
òôΩo1
I like that sequence of o-style chars (òôΩo).
Explanation (unpacked):
cr\m|+ Full program, implicit input e.g.: [1, 2, 3]
cr Copy and reverse [1, 2, 3] [3, 2, 1]
\ Zip [[1, 3], [2, 2], [3, 1]]
m|+ Map sum of array [4, 4, 4]
Implicit output
Casio-Basic, 25 bytes
l+seq(l[x],x,dim(l),1,-1
Addition threads over lists by default - but there's no built-in for reversing a list! seq is used to generate the reversed list by indexing backwards through it.
24 bytes for the function, +1 byte to add l in the parameters box.
8th, 22 bytes
Code
clone a:rev ' n:+ a:op
SED (Stack Effect Diagram) is: a -- a
Ungolfed code with comments
: f \ a -- a
clone \ clone array on TOS
a:rev \ reverse array
' n:+ a:op \ Add the corresponding elements of each array producing a new array
;
Usage
ok> [8,92] clone a:rev ' n:+ a:op .
[100,100]
Test cases
ok> [8,92] f .
[100,100]
ok> [1,2,3] f .
[4,4,4]
ok> [5,24,85,6] f .
[11,109,109,11]
ok> [] f .
[]
ok> [999] f .
[1998]
MY, 49 48 5 bytes
ωω⌽+←
How?
First, you bang your head against the wall after remembering that addition on strings isn't commutative. Then, you bang your head yet again after realizing that you could've used ω to save over 40 bytes! Finally, you arrive at this:
ω - First command line argument
ω⌽ - First command line argument, reversed
+ - Add (vectorizes)
← - Output without a newline
Java 8, 61 57 56 53 bytes
a->{for(int l=0,r=a.length;l<r;a[l]=a[--r]+=a[l++]);}
-1 byte and bug-fixed thanks to @Nevay.
-3 bytes thanks to @OliverGrégoire.
(It was a port of (and golfed by 4 8 bytes) of @jkelm's C# answer, but now it's a different shorter solution thanks to @OliverGrégoire.)
Explanation:
The method modifies the input-array to save bytes, so no need for a return-type.
a->{ // Method with integer-array parameter and no return-type
for(int l=0, // Left-integer (starting at 0)
r=a.length; // Right-integer (starting at the length of the input-array)
l<r; // Loop as long as left is smaller than right
a[l]= // Change the number at the current left index to:
a[--r]+=a[l++] // The number at the right index + itself
// (The += adds the number at the left index also to the right index)
// (And the --/++ increases/decreases the indexes by 1,
// until we've reached the middle of the array)
); // End of loop
} // End of method
C# (.NET Core),65 57 bytes
-8 bytes thanks to Kevin Cruijssen
n=>{for(int i=0,o=n.Length;i<o/2;)n[i]=n[o-1-i]+=n[i++];}
Husk, 4 bytes
Sz+↔
Try it online! (Note if you want to test with empty list, you need to supply: []:LN as argument)
Ungolfed/Explanation
-- implicit input list - [1,2,3]
S -- "duplicate input and" - [1,2,3] [1,2,3]
↔ -- reverse one copy - [1,2,3] [3,2,1]
z+ -- zip with original using + - [1+3,2+2,3+1]
C++14, 57 bytes
As generic unnamed lambda requiring a container and returns via modifying its input:
[](auto&L){auto R=L;auto r=R.end();for(auto&x:L)x+=*--r;}
Ungolfed:
[](auto&L){
auto R=L; //copy of the list
auto r=R.end();//iterator of last+1
for(auto&x:L) //foreach
x+=*--r; //move iterator to front and sum
}
Clojure, 20 17 bytes
3 bytes saved thanks to @MattPutnam
#(map +(rseq %)%)
Seems to be quite competitive with non-golfing languages.
Swift 3, 30 bytes
{zip($0,$0.reversed()).map(+)}
Swift, 76 68 58 bytes (returns an Array)
-8 bytes thanks to @Alexander.
func f(l:[Int]){print(zip(l,l.reversed()).map(+))}
Swift, 76 66 bytes
func f(l:[Int]){for i in 0..<l.count{print(l.reversed()[i]+l[i])}}
Prints the sums separated by a newline. For [1,2,3], the output is:
4
4
4
JavaScript (ES6), 27 bytes
a=>[...a].map(e=>e+a.pop())
f=
a=>[...a].map(e=>e+a.pop())
console.log(f([8, 92])) // [100, 100]
console.log(f([1, 2, 3])) // [4, 4, 4]
console.log(f([5, 24, 85, 6])) // [11, 109, 109, 11]
console.log(f([])) // []
console.log(f([999])) //[1998]
Scala, 49 bytes
(a:Seq[Int])=>a.zip(a.reverse)map{case(a,b)=>a+b}
Lambda. Paste in the REPL, it gets assigned to a default variable, like res0 or whatever. Note the variable name from the output produced after pasting that line into the REPL and pressing Enter, let's say it's res4. To call the lambda, just use res4(Seq(1, 2, 3)) or res4(Seq(89, 88, 1, 2, 3, 1000, 2000)) for the test cases.
Implementation notes:
zip creates a Tuple2 from 2 Seqs, which need to be unpacked in the pattern match (the case expression after the map) before the elements can be added. Indexed access is convoluted for golfing in Scala...
Here's another version, using convoluted indexing tricks for 57 bytes:
(a:Seq[Int])=>{val l=a.size-1;0.to(l)map(i=>a(i)+a(l-i))}
This just produces a new Seq from adding the "complementary" elements of the Seqs (symmetrically about the mid of the Seq. Very imperative.
I don't like it either, but the type ascriptions are necessary to get the REPL to accept it, otherwise it just botches out Missing Parameter Type.
The idea in the 2nd version can be used in Java 8 as well, just swapping in IntStream.rangeClosed(0,l) instead of Scala's implicit to on Ints, unlike the former, which would be considerably more convoluted in Java.
Note that @jkelm's answer for C# can be trivially translated to Java.
R, 17 16 bytes
-1 byte thanks to djhurio
rev(l<-scan())+l
Reads from stdin; returns the vector; numeric(0) is the zero-length numeric vector for the empty list.
PowerShell, 40 32 bytes
($n=$args)|%{$n[-++$i]+$n[$i-1]}
Takes input as individual command-line arguments, which is allowed as one of the native list format for PowerShell. Then loops through each element (i.e., a shorter way of looping through the indices), adding the element counting from the back (-1 indexed) to the current element (0 indexed, hence the decrement -1). Those are left on the pipeline and output is implicit.
Saved 8 bytes thanks to @briantist
C# (.NET Core), 61 60 bytes
-1 byte thanks to TheLethalCoder
a=>a.Reverse().Zip(a,(x,y)=>x+y).ToArray()
Byte count also includes:
using System.Linq;
For explanation - Zip function in LINQ takes two collections and executes given function for all corresponding elements, ie. both first elements together, both second elements etc.
Common Lisp, SBCL, 33 bytes
Pretty simple solution (very similiar to Clojure one):
(lambda(x)(mapcar'+(reverse x)x))
Example of use:
((lambda(x)(mapcar'+(reverse x)x))'(1 2 3)))
Try it online!(with print function, to show output from function)
x86 Machine Code (32-bit protected mode), 19 bytes
8D 34 B7 39 F7 73 0C 83 EE 04 8B 07 03 06 89 06 AB EB F0 C3
The above bytes of code define a function that takes two parameters (a pointer to the array in the EDI register, and the length of the array in the ESI register), modifies the pointed-to array to contain the "reverse array sum", and then returns. It does not return a value to the caller.
(This is a custom calling convention used to receive the arguments. It is actually the standard calling convention used on Gnu/Unix systems for x86-64 binaries, but in x86-32, arguments are typically passed on the stack. That takes more bytes to encode, and is less efficient, so we want to ensure that the arguments are passed to us in registers. As far as I understand the rules, this is completely legal. We don't need to conform to a particular standardized calling convention. Certainly, when writing assembly, the programmer is free to define her own calling conventions, unless she needs to interoperate with C code.)
Note that this function also assumes the direction flag is cleared (DF == 0) upon entry to the function. This is a sensible assumption, as it is guaranteed by most platform ABIs, including the Linux x86 32-bit ABI. If you need the code to work under circumstances where the state of DF cannot be assumed, then you need to add a 1-byte CLD instruction to the top of the function.
Ungolfed assembly mnemonics:
; void ReverseArraySum(int *pArray, int length);
8D 34 B7 lea esi, [edi+esi*4] ; compute back pointer
Loop:
39 F7 cmp edi, esi
73 0C jae End ; finished when EDI >= ESI
83 EE 04 sub esi, 4
8B 07 mov eax, DWORD PTR [edi]
03 06 add eax, DWORD PTR [esi]
89 06 mov DWORD PTR [esi], eax
AB stosd ; equivalent to 'mov DWORD PTR es:[edi], eax' + 'add edi, 4'
EB F0 jmp Loop
End:
C3 ret
Conceptually, the code is pretty straightforward. We just iterate through the array using two pointers. The first pointer is passed to us as an argument, in the register EDI, and it is a pointer to the beginning of the array. The second pointer is computed (initial LEA instruction) by adding the address of the beginning of the array to the length of the array, scaled by the size of an element in the array (4 bytes for an int on x86-32). Thus, ESI is a pointer to the end of the array.
At the top of the Loop, we check the pointers to see if we should keep looping or if we are finished. Normally, we'd want to save bytes by putting this test at the end of the loop (and eliminating the JMP you see at the end now), but we can't do that because the challenge requires us to handle an empty input array.
Inside the body of the loop, we:
- Eagerly subtract 4 bytes (the size of a single element in the array) from the back pointer,
ESI. - Retrieve and store the value of the element pointed to by the front pointer (
EDI) in a temporary register (EAX). - Increment
EAXby the value of the element pointed to by the back pointer (ESI). - Store the sum (
EAX) in the element pointed to by the back pointer (ESI). - Store the same sum (
EAX) in the element pointed to by the front pointer (EDI), while simultaneously incrementing that pointer (EDI) by 4 bytes (the size of a single element in the array). The handy x86 string instructionSTOSDis what allows us to do both of those things in only a single byte of code.
(With the caveat given at the outset, that the direction flag is cleared, and also the assumption that we are running in flat mode, where the "extra" segment (ES) is identical to the data segment (DS).)
Finally, we're done and we return to the caller, without returning any value.
__
You can see it run on TIO, but note that the TIO link uses a C wrapper to exercise the machine code, and GCC won't necessarily respect our custom calling convention, so we have to add extra code at the top of the function to retrieve the parameters from the stack and put them into the appropriate registers.
PHP, 59 bytes
for(;a&$c=$argv[++$i];)$a[]=$c+$argv[$argc-$i];print_r($a);
takes input from command line arguments; empty output for empty input
Yields a warning in PHP>7.0. This version does not (60 bytes):
for(;++$i<$argc;)$a[]=$argv[$i]+$argv[$argc-$i];print_r($a);
Jelly, 2 bytes
+U
or
+Ṛ
Try it online! (thanks @Mr. Xcoder for the second program)
explanation, though it's pretty self-explanatory
+U Main link
+ Add, vectorizing,
U the input, reversed
+Ṛ Main link
+ Add, vectorizing,
Ṛ the input, reversed, without vectorizing (same thing for depth-1 lists)
For empty array [], this outputs nothing. That is correct. Jelly's representation of an empty list is just simply nothing. Note that Jelly's representation of a list with a single element is just the element itself. Append ŒṘ to the code to see the Python internal representation of the output.
MATL, 3 bytes
tP+
Extremely straightforward. t duplicates the input. P flips (reverses) it, and + adds the two arrays element wise.
J, 5 bytes
(+|.)
(+|.) 1 2 3 ===> 4 4 4
(+|.) 89 98 1 2 3 1000 2000 =====> 2089 1098 4 4 4 1098 2089
NB. Does not work without the parentheses as per previous answer by @Jonah
JavaScript (ES6), 34 33 bytes
Saved a byte thanks to @ETHproductions.
a=>a.map((e,i)=>e+a[a.length+~i])
let f=
a=>a.map((e,i)=>e+a[a.length+~i])
console.log(f([8,92]));
console.log(f([1,2,3]));
console.log(f([5, 24, 85, 6]));
console.log(f([]));
console.log(f([999]));
Retina, 31 bytes
\d+
$*1;$&$*
O$^`;1+
;
1+
$.&
Try it online! Explanation:
\d+
$*1;$&$*
Convert to unary and duplicate each element.
O$^`;1+
Reverse the order of the duplicate elements.
;
Add the elements to their reversed duplicates.
1+
$.&
Convert the elements back to decimal.
Röda, 22 bytes
{reverse(_)<>_1|[_+_]}
This is an anonymous function that takes in an array and returns a stream of values, which the TIO link outputs separated over newlines.
Explanation
reverse(_) The array reversed
<> interleaved with
_1 the array itself
Push each element to the stream
[_+_] Pull two values and push their sum
C, 49 bytes
f(a,n,b)int*a,*b;{for(b=a+n;a<b--;a++)*a=*b+=*a;}
Python 2, 32 bytes
lambda l:map(sum,zip(l,l[::-1]))
Alternative solution without zip (35 bytes):
lambda l:map(int.__add__,l,l[::-1])
Neim, 2 bytes
This is a function that takes input on the top of the stack and outputs on the top of the stack.
𝕓𝔻
Racket, 30 bytes
(lambda(l)(map +(reverse l)l))
This is simply an anonymous function that maps corresponding elements from l and l reversed to the + function, returning the results in a list.
Python 2 + numpy, 31 bytes
I think this is kosher based on the rules, but let me know if not.
Anonymous function takes a numpy array as input:
import numpy
lambda l:l+l[::-1]
Japt, 7 bytes
mÈ+Ug~Y
Try it online! with the -Q flag to format the output array.
Explanation
Implicit: U = input array
mÈ
Map the input by the following function...
+Ug
The value, plus the value in the input array at index...
~Y
-(index+1), which gets elements from the end of the array.
anyfix, 3 bytes
"U+
The version on TryItOnline! is an outdated version of anyfix, which contains a few fatal errors such as not being able to add lists because of typos in the source code. Use the code on GitHub instead.
"U+ Program
" Duplicate top of stack
U Reverse top of stack if it is a list (which it should be...)
+ Add, vectorizing for lists
Positron, 59 bytes
->{a=[]i=0;while(i<len@$1)do{a+=[$1@i+($1@(-(i+1)))]i++};a}
This has many golfing opportunities that have been ruined by Positron's brokenness :P
Python 2, 40 bytes
lambda l:[i+j for i,j in zip(l,l[::-1])]
The other, shorter Python answer replaces a list comprehension with map. Wish I'd thought to do that faster. ;-;
Haskell, 20 bytes
5 bytes save by changing to a point free as suggested by nimi
zipWith(+)=<<reverse