Your program should take an array as input.
The array:
- Will always be 1 dimensional
- Will only contain integers
- Can be empty
The program should reverse the array, and then add up the elements to the original for example:
Input: [1, 2, 3]
Original: [1, 2, 3]
Reversed: [3, 2, 1]
[1, 2, 3]
+ + +
[3, 2, 1]
[1+3, 2+2, 3+1]
Output: [4, 4, 4]
Test Cases:
#In #Out
[8, 92], [100, 100]
[1, 2, 3], [4, 4, 4]
[5, 24, 85, 6], [11, 109, 109, 11]
[], []
[999], [1998]
This is code-golf, the shortest code (in bytes) wins!
-
\$\begingroup\$ J 3 bytes. Program is t. t=:+|. \$\endgroup\$Richard Donovan– Richard Donovan2019年02月10日 19:13:00 +00:00Commented Feb 10, 2019 at 19:13
-
1\$\begingroup\$ @RichardDonovan Nice Answer! Can you submit as an answer instead of a comment please :) \$\endgroup\$Noah Cristino– Noah Cristino2019年02月11日 21:10:55 +00:00Commented Feb 11, 2019 at 21:10
74 Answers 74
Haskell, 20 bytes
5 bytes save by changing to a point free as suggested by nimi
zipWith(+)=<<reverse
-
4\$\begingroup\$ go pointfree:
zipWith(+)=<<reverse
. \$\endgroup\$nimi– nimi2017年07月24日 18:19:31 +00:00Commented Jul 24, 2017 at 18:19 -
\$\begingroup\$ @nimi Wow I didn't think to do that but thats pretty smart. \$\endgroup\$2017年07月24日 18:21:12 +00:00Commented Jul 24, 2017 at 18:21
-
\$\begingroup\$ Where do I put the array? I tried arguments, and input on TIO \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:23:35 +00:00Commented Jul 24, 2017 at 18:23
-
\$\begingroup\$ @NoahCristino I fixed the TIO. This is a point-free function so you just put the input after the function however Haskell requires a
main
to compile. \$\endgroup\$2017年07月24日 18:26:39 +00:00Commented Jul 24, 2017 at 18:26 -
3\$\begingroup\$ @maple_shaft: we use
=<<
from the function monad which is defined as:(=<<) f g x = f (g x) x
. Here, written in infix:(zipWith(+) =<< reverse) x
->zipWith(+) (reverse x) x
. \$\endgroup\$nimi– nimi2017年07月24日 19:23:47 +00:00Commented Jul 24, 2017 at 19:23
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.
-
\$\begingroup\$ I found 2 issues. 1) When tested on
[9]
it outputs 18 instead of[18]
, and 2) when tested on[]
it doesn't output anything. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:19:09 +00:00Commented Jul 24, 2017 at 18:19 -
\$\begingroup\$ @NoahCristino It's not a full program, and there's already an explanation for that in the answer. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月24日 18:19:39 +00:00Commented Jul 24, 2017 at 18:19
-
\$\begingroup\$ So I guess this is fine, it's just how Jelly outputs it \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:23:01 +00:00Commented Jul 24, 2017 at 18:23
-
\$\begingroup\$ @NoahCristino Yeah. I added a part to the end of my answer so you can put that atom at the end of the code to see how Python would print it. \$\endgroup\$2017年07月24日 18:24:15 +00:00Commented Jul 24, 2017 at 18:24
-
\$\begingroup\$
+Ṛ
works too. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月24日 18:46:20 +00:00Commented Jul 24, 2017 at 18:46
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]
-
\$\begingroup\$ Oh, man, I was almost there, but I didn't think to use the spread operator to do the clone. \$\endgroup\$Rick Hitchcock– Rick Hitchcock2017年07月24日 19:09:03 +00:00Commented Jul 24, 2017 at 19:09
-
\$\begingroup\$ Can you add the embedded try it for javascript? \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 23:25:12 +00:00Commented Jul 24, 2017 at 23:25
-
\$\begingroup\$ Passed all my tests! \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:21:49 +00:00Commented Jul 24, 2017 at 18:21
-
\$\begingroup\$
R+
also works for 2 bytes. \$\endgroup\$Riley– Riley2017年07月24日 18:41:56 +00:00Commented Jul 24, 2017 at 18:41
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])
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. ;-;
-
1\$\begingroup\$ Passes all my tests! \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:20:43 +00:00Commented Jul 24, 2017 at 18:20
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.
-
1\$\begingroup\$ Good job! I like the multiple input thing! \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:45:02 +00:00Commented Jul 24, 2017 at 18:45
-
1\$\begingroup\$ I really like the multiple-input Japt interpreter. Nice job! \$\endgroup\$Oliver– Oliver2017年07月24日 19:33:14 +00:00Commented Jul 24, 2017 at 19:33
-
\$\begingroup\$ That's really cool :-) I'd add that feature to the "official" interpreter, but with the current design it's sorta unextendable... \$\endgroup\$ETHproductions– ETHproductions2017年07月24日 21:08:54 +00:00Commented Jul 24, 2017 at 21:08
-
\$\begingroup\$ Passed all my tests good job :) \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 20:26:56 +00:00Commented Jul 24, 2017 at 20:26
-
\$\begingroup\$ What an unusual method.
l*1
saves a byte. \$\endgroup\$xnor– xnor2017年07月24日 20:53:07 +00:00Commented Jul 24, 2017 at 20:53 -
\$\begingroup\$ I am having difficulties understanding the par
l*1
, any elaboration \$\endgroup\$dhssa– dhssa2017年07月26日 08:23:11 +00:00Commented Jul 26, 2017 at 8:23 -
\$\begingroup\$ @dhssa
l*1
makes a copy of the listl
. If we would not make a copy,pop()
would delete elements from the list before they were accessed in the for loop. \$\endgroup\$ovs– ovs2017年07月26日 08:30:42 +00:00Commented Jul 26, 2017 at 8:30 -
\$\begingroup\$ Thanks for the explanation, I got it now. good trick to know for coding. \$\endgroup\$dhssa– dhssa2017年07月26日 23:33:07 +00:00Commented Jul 26, 2017 at 23:33
-
1\$\begingroup\$ Passes all cases, cool name. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:37:06 +00:00Commented Jul 24, 2017 at 18:37
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.
-
1\$\begingroup\$ Good answer. Thanks for the comment about the input. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 23:23:34 +00:00Commented Jul 24, 2017 at 23:23
-
1\$\begingroup\$ Hello and welcome to PPCG! You don't need the trailing semi colon in the byte count. I believe you can return the collection straight from the
Zip
call to so no need for theToArray()
. Nice job! \$\endgroup\$TheLethalCoder– TheLethalCoder2017年07月25日 08:29:15 +00:00Commented Jul 25, 2017 at 8:29 -
\$\begingroup\$ @TheLethalCoder Thanks, and I added ToArray() since the challenge is about arrays, so I wanted the self-contained lambda to be array -> array. \$\endgroup\$Grzegorz Puławski– Grzegorz Puławski2017年07月25日 11:47:27 +00:00Commented Jul 25, 2017 at 11:47
-
\$\begingroup\$ It outputs
""
for[]
, that's weird. Not your fault just the language. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:28:17 +00:00Commented Jul 24, 2017 at 18:28 -
\$\begingroup\$ @NoahCristino That's CJam's representation of
[]
. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月24日 18:29:39 +00:00Commented Jul 24, 2017 at 18:29 -
\$\begingroup\$ Yup, it's hard to have a standard output for my challenge since many languages display arrays differently especially
[]
, and[4]
, so it's fine. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:30:55 +00:00Commented Jul 24, 2017 at 18:30
-
1\$\begingroup\$ ninja'd. I need to move to an APL keyboard... xD \$\endgroup\$Uriel– Uriel2017年07月24日 18:33:47 +00:00Commented Jul 24, 2017 at 18:33
-
\$\begingroup\$ @Uriel I am always on my APL keyboard :D \$\endgroup\$user41805– user418052017年07月24日 18:35:23 +00:00Commented Jul 24, 2017 at 18:35
-
\$\begingroup\$ Love the simplicity. It outputs
⌽+⊢
with no input \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:39:18 +00:00Commented Jul 24, 2017 at 18:39 -
\$\begingroup\$ @NoahCristino An empty input is specified with
⍬
, the empty vector. With no argument, it prints the train by itself \$\endgroup\$user41805– user418052017年07月24日 18:39:53 +00:00Commented Jul 24, 2017 at 18:39
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.
-
\$\begingroup\$ For an empty "array" it returns
numeric(0)
\$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:36:36 +00:00Commented Jul 24, 2017 at 18:36 -
1\$\begingroup\$ @NoahCristino an empty vector is represented as
numeric(0)
in R. \$\endgroup\$Leaky Nun– Leaky Nun2017年07月24日 18:58:42 +00:00Commented Jul 24, 2017 at 18:58 -
\$\begingroup\$ That's fine. @LeakyNun \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 19:01:34 +00:00Commented Jul 24, 2017 at 19:01
-
1\$\begingroup\$
rev(l<-scan())+l
, 16 bytes? \$\endgroup\$djhurio– djhurio2017年07月25日 13:08:30 +00:00Commented Jul 25, 2017 at 13:08 -
\$\begingroup\$ For the record, an R+pryr functional alternative is just one byte longer:
pryr::f(rev(x)+x)
\$\endgroup\$JayCe– JayCe2018年05月03日 18:21:58 +00:00Commented May 3, 2018 at 18:21
Clojure, (削除) 20 (削除ここまで) 17 bytes
3 bytes saved thanks to @MattPutnam
#(map +(rseq %)%)
Seems to be quite competitive with non-golfing languages.
-
\$\begingroup\$ Use
rseq
instead ofreverse
. \$\endgroup\$MattPutnam– MattPutnam2017年07月26日 19:07:02 +00:00Commented Jul 26, 2017 at 19:07
-
\$\begingroup\$ Yay passes all my tests! \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:20:04 +00:00Commented Jul 24, 2017 at 18:20
-
\$\begingroup\$ Equivalent:
sV_
\$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月24日 18:25:56 +00:00Commented Jul 24, 2017 at 18:25 -
\$\begingroup\$ @Mr.Xcoder nevermind \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月24日 18:28:58 +00:00Commented Jul 24, 2017 at 18:28
-
\$\begingroup\$ Can you add a TIO? and a link under the name like this one: codegolf.stackexchange.com/a/135427/61877 \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:48:32 +00:00Commented Jul 24, 2017 at 18:48
-
\$\begingroup\$ @NoahCristino: Like this? Haven't used that thing so far, so no idea what I may have done wrong. By the way, if you expect people to use a certain service in their answers, then please state so in the task description. \$\endgroup\$Joey– Joey2017年07月24日 18:52:36 +00:00Commented Jul 24, 2017 at 18:52
-
\$\begingroup\$ That's fine. It's not required it just makes the answers more high quality, and easier to test out for future viewers. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:54:14 +00:00Commented Jul 24, 2017 at 18:54
C, 49 bytes
f(a,n,b)int*a,*b;{for(b=a+n;a<b--;a++)*a=*b+=*a;}
-
\$\begingroup\$ Shouldn't
a,n,b
bea,b,n
or something? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月24日 20:12:28 +00:00Commented Jul 24, 2017 at 20:12 -
\$\begingroup\$ @EriktheOutgolfer No,
b
isn't a parameter for the function, just an extra definition stuffed in there for golfing reasons.a
must be a pointer to integers, andn
must be how many integers there are in the array. \$\endgroup\$orlp– orlp2017年07月24日 20:13:50 +00:00Commented Jul 24, 2017 at 20:13 -
\$\begingroup\$ Could you please add a TIO link? \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 20:27:27 +00:00Commented Jul 24, 2017 at 20:27
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
-
\$\begingroup\$ it doesn't output an array. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:27:23 +00:00Commented Jul 24, 2017 at 18:27
-
1\$\begingroup\$ @NoahCristino PowerShell input/output is, in general, weird. It is outputting as an array, it's just there's nothing capturing said array, and so when the implicit
Write-Output
happens, it puts things on stdout one item per line. For example, you can see here that, when captured, the object is indeed anarray
type. \$\endgroup\$AdmBorkBork– AdmBorkBork2017年07月24日 18:31:22 +00:00Commented Jul 24, 2017 at 18:31 -
\$\begingroup\$ Good enough then :) atleast it tried \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:38:11 +00:00Commented Jul 24, 2017 at 18:38
-
\$\begingroup\$ I'm on mobile and can't test so easily, but isn't it 1 byte shorter to remove the
param
and then replace1..$n
with1..($n=$args)
? \$\endgroup\$briantist– briantist2017年07月24日 21:51:38 +00:00Commented Jul 24, 2017 at 21:51 -
\$\begingroup\$ @briantist Not quite, but you did give me a different way of thinking about it, saving a bunch of bytes. Thanks! \$\endgroup\$AdmBorkBork– AdmBorkBork2017年07月25日 12:54:55 +00:00Commented Jul 25, 2017 at 12:54
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
-
1\$\begingroup\$ You can save 1 byte by using
a->{for(int i=0,l=a.length;i<l/2;a[i]=a[l+~i]+=a[i++]);}
. \$\endgroup\$Nevay– Nevay2017年08月16日 10:56:05 +00:00Commented Aug 16, 2017 at 10:56 -
1\$\begingroup\$ Besides that the code fails for arrays with an odd length
1,2,3
(returns4,2,4
instead of4,4,4
), the loop has to run as long as2*i<l
, noti<l/2
. \$\endgroup\$Nevay– Nevay2017年08月16日 11:03:40 +00:00Commented Aug 16, 2017 at 11:03 -
\$\begingroup\$ @Nevay Thanks. I knew it should be possible to golf
l-i-1
, just couldn't come up with it.. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年08月16日 11:10:37 +00:00Commented Aug 16, 2017 at 11:10 -
2\$\begingroup\$ 53 bytes:
a->{for(int l=0,r=a.length;l<r;a[l]=a[--r]+=a[l++]);}
. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年08月16日 12:05:37 +00:00Commented Aug 16, 2017 at 12:05 -
1\$\begingroup\$ @OlivierGrégoire Thanks. And your
l
andr
makes sense for your implementation, so I've used those as well (and added an explanation). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年08月16日 12:58:05 +00:00Commented Aug 16, 2017 at 12:58
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
-
\$\begingroup\$ Nice! I like the line by line explanation \$\endgroup\$Noah Cristino– Noah Cristino2018年05月03日 20:46:24 +00:00Commented May 3, 2018 at 20:46
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 }
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.
-
2\$\begingroup\$
map*
is your friend here: Try it online! \$\endgroup\$DLosc– DLosc2022年02月02日 22:43:52 +00:00Commented Feb 2, 2022 at 22:43
-
\$\begingroup\$ It outputs floats, but I never said it couldn't, so that's fine, and for
[]
it doesn't output anything but that's just the language. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:26:36 +00:00Commented Jul 24, 2017 at 18:26
-
\$\begingroup\$ Perfect, great job. \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:43:05 +00:00Commented Jul 24, 2017 at 18:43
-
1\$\begingroup\$ Erik, solving my problem in like 7 languages xD \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:43:32 +00:00Commented Jul 24, 2017 at 18:43
-
\$\begingroup\$ @NoahCristino Yeah I'm trying to keep it under 15...;) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月24日 18:44:16 +00:00Commented Jul 24, 2017 at 18:44
-
\$\begingroup\$ I'm going to have to start adding a -1 for every submission xD \$\endgroup\$Noah Cristino– Noah Cristino2017年07月24日 18:46:16 +00:00Commented Jul 24, 2017 at 18:46
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