37
\$\begingroup\$

Your program should take an array as input.

The array:

  1. Will always be 1 dimensional
  2. Will only contain integers
  3. 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 , the shortest code (in bytes) wins!

\$\endgroup\$
2
  • \$\begingroup\$ J 3 bytes. Program is t. t=:+|. \$\endgroup\$ Feb 10, 2019 at 19:13
  • 1
    \$\begingroup\$ @RichardDonovan Nice Answer! Can you submit as an answer instead of a comment please :) \$\endgroup\$ Feb 11, 2019 at 21:10

68 Answers 68

1 2
3
0
\$\begingroup\$

Tcl, 49 bytes

lmap l $L r [lreverse $L] {lappend N [incr l $r]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 14 bytes

a->a+Vecrev(a)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

R: 19 bytes

function(v)rev(v)+v

Try It Out: http://www.r-fiddle.org/#/fiddle?id=vJhwqK77

\$\endgroup\$
1
  • \$\begingroup\$ Hello and welcome to the site. I don't know R very well This looks like what is called a snippet which we do not allow for answers here. Your answer may be either a function (anonymous or named) or a full program. \$\endgroup\$
    – Wheat Wizard
    Jul 26, 2017 at 5:31
0
\$\begingroup\$

Clojure, 21 bytes

#(map + %(reverse %))

Try it online!

Having trouble golfing this any further. Any clever ideas?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ There's already a similar answer a little ahead of you. Flip the last two args to eliminate a space, and use rseq instead of reverse. \$\endgroup\$
    – MattPutnam
    Jul 26, 2017 at 19:09
0
\$\begingroup\$

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]
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 24 + 1 (-a) = 25 bytes

print$_+$F[--$i],$"for@F

Try it online!

\$\endgroup\$
-3
\$\begingroup\$

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
\$\endgroup\$
1
  • 5
    \$\begingroup\$ While there's no shame in arriving independently at an answer that is identical to someone else's, this isn't the case here. +|. is valid on its own, so this is just a less golfed version of Jonah's answer. \$\endgroup\$
    – Dennis
    Jul 25, 2017 at 0:55
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.