11
\$\begingroup\$

Task

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]

Image:

enter image description here

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]
\$\endgroup\$
4
  • 3
    \$\begingroup\$ Does this answer your question? Ken Iverson’s Favourite APL Expression? \$\endgroup\$
    – Bgil Midol
    Jan 20 at 21:00
  • \$\begingroup\$ No, this doesn't answer. \$\endgroup\$
    – Fmbalbuena
    Jan 20 at 21:02
  • \$\begingroup\$ Some golflangs can't do with bulitins, this is different. \$\endgroup\$
    – Fmbalbuena
    Jan 20 at 21:06
  • 5
    \$\begingroup\$ 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 \$\endgroup\$ Jan 20 at 21:07

18 Answers 18

10
\$\begingroup\$

Jelly, 3 bytes

ŻŻ+

Try it online!

Using hyper's oberservation

How it works

ŻŻ+ - Main link. Takes a list L
ŻŻ  - Prepend two zeros
  + - Add elementwise
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Given that hyper stole my answer before ;) \$\endgroup\$ Jan 20 at 21:02
  • \$\begingroup\$ Does Jelly work here like J, adding the zero fill automatically to the bottom row: example in J? \$\endgroup\$
    – Jonah
    Jan 20 at 23:21
  • 3
    \$\begingroup\$ @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] \$\endgroup\$ Jan 20 at 23:27
  • \$\begingroup\$ Ah, ok. So a different mechanism but the same result. \$\endgroup\$
    – Jonah
    Jan 20 at 23:32
6
\$\begingroup\$

Python 3, 38 bytes

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

Try it online!

Also works in Python 2

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

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 }
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

R, 28 bytes

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

Try it online!

Updates the related challenge.

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!

\$\endgroup\$
1
  • \$\begingroup\$ @alephalpha quite right. Fixed, thanks. \$\endgroup\$
    – Giuseppe
    Jan 21 at 12:54
3
\$\begingroup\$

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

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}

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

convey, 14 13 bytes

0+}
^,<<0
0"{

Try it online!

Program running with 1 2 3

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

Ruby, 34 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

05AB1E (legacy), 5 bytes

2Å0ì+

Try it online or verify all test cases.

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

Try it online or verify all test cases.

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)
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 34 bytes

Saved 2 bytes thanks to @tsh

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

Try it online!

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

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

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 15 bytes

F²⊞θ⁰IEθ⁺ι§θ⁻κ²

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

F²⊞θ⁰

Append two zeros to the input array.

IEθ⁺ι§θ⁻κ²

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

\$\endgroup\$
1
\$\begingroup\$

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.
    lodsd           # Load the current number into EAX, 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,
                    #  advancing the pointer.
    xchg edx, eax   # Switch the last number into EAX.
    stosd           # Write the last number to the result array, advancing the pointer.
    ret             # Return.
\$\endgroup\$
1
\$\begingroup\$

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)
  +     #     by addition:
   ‼    # g = apply twice
    Θ   #     prepend zero

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

\$\endgroup\$
1
\$\begingroup\$

BQN, 14 12 10 bytes

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

2⊸⌽⊸+0‿0∾⊢

Try it at BQN online REPL

\$\endgroup\$
8
  • \$\begingroup\$ Here are a few shorter functions, 10 to 13 bytes \$\endgroup\$
    – ovs
    Jan 21 at 23:24
  • \$\begingroup\$ @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... \$\endgroup\$ Jan 21 at 23:30
  • \$\begingroup\$ @ovs - Starting from your 10-byter, would this be a valid 9-byte program? \$\endgroup\$ Jan 21 at 23:40
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Razetime
    Jan 22 at 9:49
  • 1
    \$\begingroup\$ a simple program would require input from STDIN, and BQN online doesn't have that. A snippet is not a valid submission. \$\endgroup\$
    – Razetime
    Jan 22 at 12:18

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.