# Move to Right and left

Your task is to take an array of numbers as input, and produce a new one where each number has been shifted both right and left, leaving 0s if no number fills a spot. This process can be represented graphically like this:

   [x]
/ | \
[x, 0, x]


This is done with every number in the list, with overlapping numbers being added. A list with two numbers would look like this:

   [x, y]
/ \/ \
/  /\  \
[x, y, x, y]


With three numbers, you start to get overlaps, and addition:

   [x,  y,  z]
/ \/   \/ \
/  /\   /\  \
[x, y, x+z, y, z]


## Example

A simple example is [1] gives [1, 0, 1] because

  [1]
/ \
/   \
[1, 0, 1]


Another example

   [1, 2]
/\ /\
/  X  \
/  / \  \
[1, 2, 1, 2]


Another example

   [1, 2, 3]
/\ /\ /\
/  X  X  \
/  / \/ \ /\
[1, 2, 4, 2, 3]


## Test cases

[1,2,3] => [1,2,4,2,3]
[4,2] => [4,2,4,2]
[1] => [1,0,1]
[7,4,5,6] => [7,4,12,10,5,6]
[1,2,4,2,1] => [1,2,5,4,5,2,1]
[1,0,1] => [1,0,2,0,1]
[1,0,2,0,1] => [1,0,3,0,3,0,1]
[8,7,6,5,4] => [8,7,14,12,10,5,4]
[1,4,9,16] => [1,4,10,20,9,16]
[1,2] => [1,2,1,2]

• Does this answer your question? Ken Iverson’s Favourite APL Expression? Jan 20 at 21:00
• No, this doesn't answer. Jan 20 at 21:02
• Some golflangs can't do with bulitins, this is different. Jan 20 at 21:06
• As I said in chat, I'm not convinced these two are duplicates. For array languages, the approach is likely to be the same, but the restriction of the input to binomial lists, along with the various properties of the binomial vs generic integer lists, means that for most languages, the approaches will be different Jan 20 at 21:07

# Jelly, 3 bytes

ŻŻ+


Try it online!

## How it works

ŻŻ+ - Main link. Takes a list L
ŻŻ  - Prepend two zeros

• Given that hyper stole my answer before ;) Jan 20 at 21:02
• Does Jelly work here like J, adding the zero fill automatically to the bottom row: example in J? Jan 20 at 23:21
• @Jonah In this case, effectively yes. When a vectorising command such as + has arguments of different length, it simply appends the remaining values of the longer argument to the end of the result. So [0, 0, a, b, c] + [a, b, c] = [0+a, 0+b, c+a, b, c] Jan 20 at 23:27
• Ah, ok. So a different mechanism but the same result. Jan 20 at 23:32

# Python 3, 38 bytes

lambda l:map(sum,zip(l+[0,0],[0,0]+l))


Try it online!

Also works in Python 2

• If you use Python 2, map produces a list: tio.run/… Jan 20 at 21:38
• -2 bytes if you accept a tuple instead of list as the argument Jan 20 at 22:22
• [*...] instead of list(...)?
– Neil
Jan 20 at 22:37
• @Fmbalbuena Aren't they the same byte count though? Jan 21 at 5:59
• You don't need the [*]; outputting a generator is perfectly fine: codegolf.meta.stackexchange.com/a/10753 Jan 21 at 8:28

# Factor + pair-rocket, 34 bytes

[ 0 => 0 prepend dup 2 rotate v+ ]


Try it online!

## Explanation

Infix syntax? In my Factor? You bet. 0 => 0 is 1 byte shorter than { 0 0 } and does the same thing.

           ! { 1 2 3 }
0 => 0     ! { 1 2 3 } { 0 0 }
prepend    ! { 0 0 1 2 3 }
dup        ! { 0 0 1 2 3 } { 0 0 1 2 3 }
2 rotate   ! { 0 0 1 2 3 } { 1 2 3 0 0 }
v+         ! { 1 2 4 2 3 }


# Haskell + hgl, 12 bytes

zdm~<ao[i,i]


## Explanation

zdm takes a SemiAlign of Semigroup elements and combines them with the semigroup action defaulting to the present value if only one is present.

That's a bit of a mouthful. In this case zdm takes two lists of numbers and zips them together with addition. In places where there is only one number to add (i.e. the longer end of a list) it just adds zero.

ao concats two lists so ao[i,i] adds two copies of i to the front. For integers i=0 so this is the same as adding zero twice. With that we combine it with a monad operator ~< and we get something that takes a list and zdms it with a version of it with two zeros on the front.

I use i here because the result is a nice function which can work on more than just lists. For example it will also combine lists or strings in the natural way.

ghci> zdm~<ao[i,i]$[1,2,3] [1,2,4,2,1] ghci> zdm~<ao[i,i]$["a","b","c"]
["a","b","ca","b","c"]


## Reflections

[i,i] is pretty long. If I made an alias Ki for K i (or K0 for K 0) this could be zdm~<Ki<Ki saving two bytes overall.

# R, 28 bytes

function(x)c(0,0,x)+c(x,0,0)


Try it online!

Thanks to alephalpha for pointing out a bug.

# R >= 4.1, 21 bytes

\(x)c(0,0,x)+c(x,0,0)


Try it online!

• @alephalpha quite right. Fixed, thanks. Jan 21 at 12:54

# J, 10 bytes

0 0&(,+,~)


Try it online!

Obligatory nod to Iverson's favorite expression, as noted in comments by Bgil Midol.

Or, for one extra byte, in the prettier symmetric form:

0 0&,+,&0 0


# Wolfram Mathematica, 18 bytes

{##,0,0}+{0,0,##}&


Just a Mathematica port of what other posters have done: Create two lists, where two zeros are appended to the input in one, and prepended to the input in the other, and add them.

Sample usage:

{##,0,0}+{0,0,##}&@@{8,7,6,5,4}


{8, 7, 14, 12, 10, 5, 4}

# convey, 14 13 bytes

0+}
^,<<0
0"{


Try it online!

# Desmos, 28 bytes

f(l)=join(l,0,0)+join(0,0,l)


Can't get much shorter than this as far as I'm aware.

Try It On Desmos!

Try It On Desmos! - Prettified

# Ruby, 34 bytes

->a{(r=0,0,*a).zip(a+r).map &:sum}


Try it online!

# Octave, 19 bytes

@(a)conv(a,[1,0,1])


Try it online!

# Octave, 19 bytes

@(a)[a,0,0]+[0,0,a]


Try it online!

# 05AB1E (legacy), 5 bytes

2Å0ì+


Uses the legacy version of 05AB1E, because the + will keep trailing items when lists are of different lengths, whereas the new 05AB1E version would ignore/remove them. In the new version of 05AB1E, this therefore would have been 7 bytes instead:

2Å0ì0ζO


Explanation:

2Å0     # Push list [0,0]
ì    # Prepend it front of the (implicit) input-list
+   # Add the values at the same positions to the input-list,
# keeping the additional trailing items
# (after which the result is output implicitly)

2Å0ì    # Same as above
ζ  # Zip/transpose with the (implicit) input-list, creating pairs,
0   # using 0 as filler-item when lists are different in length
O # Sum each inner pair
# (after which the result is output implicitly)


# JavaScript (ES6), 34 bytes

Saved 2 bytes thanks to @tsh

a=>[0,0,...a].map((v,i)=>v+~~a[i])


Try it online!

• a=>[0,0,...a].map((v,i)=>v+~~a[i])
– tsh
Jan 21 at 6:18

# T-SQL, 94 bytes

SELECT sum(x)FROM(SELECT*FROM @
UNION SELECT x,i+2FROM @
UNION SELECT 0,i+1FROM @)y
GROUP BY i


Try it online

# Charcoal, 15 bytes

Ｆ²⊞θ⁰ＩＥθ⁺ι§θ⁻κ²


Try it online! Link is to verbose version of code. Explanation:

Ｆ²⊞θ⁰


Append two zeros to the input array.

ＩＥθ⁺ι§θ⁻κ²


Add the last but one element to each element of the array and output the result.

# x86-64 machine code, 16 bytes

89 d1 31 c0 99 03 06 ab ad 92 e2 f9 ab 92 ab c3


Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address at which to place the result, as an array of 32-bit integers; the address of the given array in RSI; and the length of the given array in RDX.

In assembly:

f:
mov ecx, edx    # Put the length in ECX.
xor eax, eax    #  | Set EAX and EDX to 0.
cdq             #  | These will hold the two previous numbers in that order,
repeat:             #  |  or 0 if nonexistent.
add eax, [rsi]  # Add the current number to the twice-previous number.
stosd           # Write the sum to the result array, advancing the pointer.
xchg edx, eax   # Exchange EDX and EAX, putting the previous number into EAX
#                            and the current number into EDX.
loop repeat     # Loop, counting down from the length in ECX.
stosd           # Write the penultimate number to the result array,
xchg edx, eax   # Switch the last number into EAX.
stosd           # Write the last number to the result array, advancing the pointer.
ret             # Return.


# Husk, 5 bytes

Sż+‼Θ


Try it online!

Zips by addition the input together with itself after prepending two zeros:

Sż+‼Θ
S       # hook: Sfgx means f(x,g(x))
ż      # f = zip together (keeping trailing elements)
‼    # g = apply twice
Θ   #     prepend zero


(one byte (‼) longer than Ken Iverson’s Favourite Expression)

# BQN, 1412 10 bytes

Edit: -2 bytes (and a BQN lesson) thanks to ovs & Razetime

2⊸⌽⊸+0‿0∾⊢


Try it at BQN online REPL

• Here are a few shorter functions, 10 to 13 bytes
– ovs
Jan 21 at 23:24
• @ovs - Thanks a lot! I managed to get down to 12 with my own efforts, at least. I was sure that the 11-byter must be possible, but didn't manage to work it out: thanks for showing me how! I still need to think a bit about the 10-byter, though... Jan 21 at 23:30
• @ovs - Starting from your 10-byter, would this be a valid 9-byte program? Jan 21 at 23:40
• 0‿0(∾˜+∾) is not a valid train. however 0‿0(∾˜+∾)⊢ and 0‿0⊸(∾˜+∾) are valid trains. The way to test this is trying to assign it. Jan 22 at 9:49
• a simple program would require input from STDIN, and BQN online doesn't have that. A snippet is not a valid submission. Jan 22 at 12:18