# Up and down, up and down

### Challenge:

Given a positive integer input n, create a vector that follows this pattern:

0  1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n


Or, explained with words: The vector starts at 0, and makes increments of 1 until it reaches the smallest odd positive integer that isn't part of the sequence, then it makes decrements until it reaches the smallest (in magnitude) even negative integer that isn't part of the sequence. It continues this way until n is reached. The sequence will end on positive n if n is odd, and negative n if n is even.

The output format is flexible.

### Test cases:

n = 1
0  1
-----------
n = 2
0  1  0 -1 -2
-----------
n = 3
0  1  0 -1 -2 -1  0  1  2  3
-----------
n = 4
0  1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4
-----------
n = 5
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


You may choose to take the n zero-indexed. n = 1 would then give 0 1 0 -1 -2.

This is , so the shortest code in each language wins! Explanations are encouraged as always!

# JavaScript (Node.js), 83 bytes

n=>[v=0].concat(...[...Array(n),c=d=-1].map(t=>[...Array(c+=2)].map(t=>v+=d,d=-d)))


Try it online!

-*N×ŒḄ)j00;ḣN‘$ Try it online! # Clean, 59 bytes import StdEnv$m=foldr(\a=(++)[~a..a]o map~)[][0..m-1]++[m]


Try it online!

Defines the function $:: Int -> [Int] generating the required list. A direct port of user1472751's Haskell solution ended up being slightly better: # Clean, 53 bytes import StdEnv$n m|m>n=[~n..n]++map~($(n+1)m)=[~m]  $0


Try it online!

# Ruby, 59 bytes

->n{a,*b=-1;n.times{|z|b+=[*(z*a).step(z*a=-a,a)]};b+[n*a]}


Try it online!

# C# (.NET Core), 97 bytes

n=>{var r="";int m=0,a=0,i=0;for(;++i<=n;)for(m=i%2>0?1:-1;a!=i*m;a+=m)r+=a+" ";r+=n*m;return r;}


Try it online!

Another different approach in C#.

# CJam, 59 bytes

1]q~:X*ee{~[\_-1\#@*](2*)*}%:+W%~[]\_@+{@@+_@+}XX*(*W%0\+p;


Try it online!

Suggestions for improvement are welcome!

# Python 3, 83 bytes

lambda n:numpy.cumsum(+sum([[(-1)**i]*(i-~i)for i in range(n)],[]))
import numpy


Try it online!

I'll throw in a numpy solution, just for fun.

# Wolfram Language (Mathematica), 56 51 bytes

-5 bytes thanks to user202729

Join@@MapAt[-#&,Table[Range[-k,k],{k,#}],1;;-1;;2]&


Try it online!

• Flatten@ can be Join@@. Table can use prefix notation. Range can be shortened by calculating the reverse range and then negate. – user202729 May 18 '18 at 4:27

Swift, 293 bytes

let odd = n % 2 != 0
var i = 0
var inc = 1
var s : [Int] = 
repeat {
i += inc
if (inc == 1 && i % 2 != 0) || (inc == -1 && i % 2 == 0) {
if !s.contains(i) {
inc = -inc
}
}
s.append(i)
} while !((odd && i == n) || (!odd && i == -n))
print("\(s)")

• Welcome to PPCG! This isn't currently a valid submission as it is neither a full program nor a function (it assumes n is a predefined variable, which isn't allowed by default), and you can save a lot of bytes by shortening variable names and removing white-space. – Οurous May 19 '18 at 2:42

# Attache, 38 bytes

Collapse@@{::&>Pairs!On[Even,-,0:_]}


Try it online!

I'm struggling to break 38 bytes... I'll post an explanation once I'm convinced I can't get lower than this (or if I golf it!).

# JavaScript (Node.js), 51 bytes

n=>{for(p=i=0;i<=n*n;p+=1-(i++**.5|0)%2*2)alert(p)}


Try it online!

I stole toke ripped off used @KevinCruijssen's answer.

Thanks to @StewieGriffin for adding one more byte to my count (I forgot to output the last number) read the comments

# Yabasic, 83 bytes

An anonymous function that takes Input, n as an integer and outputs to the console.

Input""n
For i=1To n
For j=-i To i-1
k=i-Abs(j)
?(-1)^(i+1)*k;
If k=n End
Next
Next


Try it online!