Skip to main content
fixed explanation
Source Link
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105

MATL, 17 15 bytes

-2 bytes thanks to Luis Mendo!

0i:oEqG:EqY"Ysh

Try it online!

Explanation for n=3:

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4 -3 -2 -1  0  1  2  3  4  5]3]]
	      h	% horizontally concatenate
		% end of program, automatically print the stack

MATL, 17 15 bytes

-2 bytes thanks to Luis Mendo!

0i:oEqG:EqY"Ysh

Try it online!

Explanation for n=3:

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4 -3 -2 -1  0  1  2  3  4  5]
	      h	% horizontally concatenate
		% end of program, automatically print the stack

MATL, 17 15 bytes

-2 bytes thanks to Luis Mendo!

0i:oEqG:EqY"Ysh

Try it online!

Explanation for n=3:

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3]]
	      h	% horizontally concatenate
		% end of program, automatically print the stack
added 627 characters in body
Source Link
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105

MATL, 1717 15 bytes

-2 bytes thanks to Luis Mendo!

0TFEqi0i:)GoEqG:EqY"Ysh

Try it online!Try it online!

MATL port of my R answer. Maybe a non-Explanation for cumsumn=3 approach is possible but I'm pretty confident this is the way to go.:

Explanation to follow.

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4 -3 -2 -1  0  1  2  3  4  5]
	      h	% horizontally concatenate
		% end of program, automatically print the stack

MATL, 17 bytes

0TFEqi:)G:EqY"Ysh

Try it online!

MATL port of my R answer. Maybe a non-cumsum approach is possible but I'm pretty confident this is the way to go.

Explanation to follow.

MATL, 17 15 bytes

-2 bytes thanks to Luis Mendo!

0i:oEqG:EqY"Ysh

Try it online!

Explanation for n=3:

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4 -3 -2 -1  0  1  2  3  4  5]
	      h	% horizontally concatenate
		% end of program, automatically print the stack
Source Link
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105

MATL, 17 bytes

0TFEqi:)G:EqY"Ysh

Try it online!

MATL port of my R answer. Maybe a non-cumsum approach is possible but I'm pretty confident this is the way to go.

Explanation to follow.