Reto:
Dada una entrada entera positiva n , cree un vector que siga este patrón:
0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n
O, explicado con palabras: el vector comienza en 0, y hace incrementos de 1hasta que alcanza el número entero impar impar más pequeño que no es parte de la secuencia, luego hace decrementos hasta que alcanza el número entero negativo más pequeño (en magnitud) que no es No es parte de la secuencia. Continúa de esta manera hasta que nse alcanza. La secuencia terminará en positivon sin es impar, y negativo nsi nes par.
El formato de salida es flexible.
Casos de prueba:
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
Puede elegir tomar la n indexada a cero. n = 1entonces daría 0 1 0 -1 -2.
Este es el código de golf , por lo que gana el código más corto en cada idioma. Se alientan las explicaciones como siempre!

Respuestas:
R,
5854504843 bytes-2 bytes thanks to MickyT
Try it online!
fuente
Perl 6,
6026 bytesTry it
Try it
Expanded:
(-1,-*...*)Z*0..$_generates the sequence0 1 -2 3 -4 5fuente
Python 2,
695756 bytesTry it online!
For each
nup to theinputtherange(-n,n)(inclusive) is calculated, inverted whennis an even number, has the fist two numbers (after the inversion) removed, and then appended to the output.fuente
05AB1E,
97 bytesSaved 2 bytes thanks to @Emigna
Try it online!
My first 05AB1E answer (I think), so I may be missing some tricks...
Explanation
I have to thank @Dennis for the original usage of
Ÿ, otherwise Imay notprobably would never have known about it...fuente
ÝεDÈi®*}}Ÿwithout checking,ā®smis crazy smart haha.05AB1E,
1514 bytesTry it online!
Explanation
fuente
JavaScript (ES6), 56 bytes
Try it online!
Commented
fuente
Haskell, 43 bytes
Try it online!
Computes the negated cumulative sums of the list
[(-1)^k|k<-[1..n],_<-[2..2*k]], which is the firstnrows offuente
Jelly,
119 bytesTry it online!
How it works
fuente
Haskell,
4842 bytesTry it online!
Thanks to Οurous for -1 byte
Even though it's kind of obvious in hindsight, it took me a while to arrive at
(-1)^i*xwhich isxwheniis even and-xwheniis odd. Previous iterations where:fuente
1-iinstead of-i+1in the..expression.C# (.NET Core),
300167 bytesI've never done any of these before, but this one seemed fun. I see why people use those "golfing" languages as 167 seems way higher than some of the other answers. But, you gotta go with what you know.
Try it online!
fuente
usingstatements and the function. This is allowed by default unless the challenge specifies it must be a full program (even if it did, you could shorten the containing class name).[](){};.)(n-1)*2is just2*n-2and with some rearrangement you can remove the parentheses there.!=has precedence less than%so you can remove a pair of parens. And you can use>0instead of `!=0, saves a byte.static int[] f(int n)can becomef=n=>by using a (recursive) lambda, and(n-1)*2can become~-n*2to save on the parenthesis. I got it down to 155 (137 + 18) bytes: Try it online. The 18 bytes are forusing System.Linq;, because required imports are mandatory for the byte-count. Enjoy your stay!J, 25 bytes
-5 bytes thanks to FrownyFrog!
Try it online!
J, 30 bytes
Explanation:
i.,]creates list 0..n&.>for each number in the list execute the verb in (...) and box the result (I need boxing because the results have different length)[:( _1&^)find -1 to theith power (-1 or 1)i:@*make a list -n..n or n..-n, depending on the sign of the above;@unbox>:@*:find n^2 + 1}.and take so many numbers from the listTry it online!
fuente
nversion? e.g*:{.;@([:(i:@*_1&^)&.>i.).. the specification allows that$for the cut-off, no need for&.>because*is rank-0.Python 2,
6556 bytesThe output format is a bit ugly. :/
Try it online!
fuente
Java 8,
858379 bytes-6 bytes thanks to @OlivierGrégoire.
Try it online.
Explanation:
fuente
j).igo up instead of down to remove a redundantn*n.R,
48 4642 bytesTry it online!
A port of the Ruby answer by Kirill L. - and saved 6 bytes thanks to the same Kirill L.! Now shorter than Giuseppe's solution ;)
A port of this Octave answer by Luis Mendo using
approxis less golfy.n=n^2+1can be replaced by,,n^2+1; or by0:n^2+1(positional argumentxout) for the same byte count :R, 56 bytes
Try it online!
fuente
approxwill work here in a similar manner to Luis Mendo's Octave solution as well.diffinvandapproxfrom this question...~doesn't work as a complement operator :(), you can still save another 2 bytes by switching to a full program.APL (Dyalog Unicode), 17 bytes
Try it online!
Golfed 2 bytes thanks to @FrownyFrog by converting to a train. See the older answer and its explanation below.
APL (Dyalog Unicode), 19 bytes
Try it online!
(Uses
⎕IO←0)My first approach was to construct multiple ranges and concatenate them together, this easily went over 30 bytes. Then I started analysing the sequence
+\⍣¯1denotes the inverse cumulative sumThere is a repeating pattern of 1s and ¯1s, where the length of each consecutive sequence of 1s or ¯1s is 1+2×n. And each subsequence alternates between 1 and ¯1. What I can do now is to create the 1s and ¯1s list, and then scan by +
fuente
+\0,¯1*⍳(/⍨)1+2×⍳is 17Haskell, 47 bytes
Try it online!
fuente
Java (JDK 10), 98 bytes
Try it online!
fuente
MATL,
1715 bytes-2 bytes thanks to Luis Mendo!
Try it online!
Explanation for
n=3:fuente
Octave,
444241 bytes2 bytes removed thanks to @StewieGriffin, and 1 byte further removed thanks to @Giuseppe!
Try it online!
fuente
Ruby,
5247 bytesTry it online!
Below is the original 52-byte version with an explanation:
Try it online!
Walkthrough
fuente
map(&:-@)portion?-r.Prolog (SWI), 113 bytes
Try it online!
fuente
Python 3, 83 bytes
fuente
Husk,
1817 bytesTry it online!
fuente
Charcoal, 19 bytes
Try it online! Link is to verbose version of code. Explanation:
Alternative explanation:
Loop over the integers from
0to the input inclusive.Cast the results to string before printing.
Negate alternate sets of results.
Form a list from the previous index to the current index, excluding the previous index.
fuente
Jelly,
1112 bytesBah, I thought I had 11 wih
_2+ỊrN)N;¥/Try it online!
How?
fuente
Stax, 10 bytes
Run and debug it
fuente
Scala, 119 Bytes
Ungolfed:
This can probably be golfed much better, but I wanted a solution utilizing lazy Streams.
fuente
APL (Dyalog Unicode),
3432 bytesTry it online!
Requires
⎕IO←0-2 bytes thanks to @FrownyFrog
fuente
Stacked, 44 bytes
Try it online! It's been a while since I programmed in Stacked, but I think I still got it.
Alternatives
73 bytes:
[0\|>:2%tmo*2 infixes[:...|>\rev...|>rev#,$#'sortby 1#behead]flatmap 0\,]This goes with the "ranges from generated indices" approach used in my Attache answer. This proved to be pretty long, since Stacked has no builtin for reversed ranges nor collapsing. (That's what
:...|>\rev...|>rev#,$#'sortby 1#beheaddoes.)53 bytes:
[0\|>:2%tmo _\tpo#,tr[...rep]flatmap 0\,inits$summap]...so I decided to go for an approach which instead finds the cumulative sum (
inits$summap) over1and-1repeated by the odd integers, as in the R answer.46 bytes:
[~>0\:2%\#,2*1-tr[...rep]flatmap,inits$summap]...but I realized that the negative integers and the odd integers could be made in one go, by multiplying both generated arrays (the mod 2 values of the range and the range itself) by
2then subtracting1. This gives alternating1s and-1s for the first range and the odd integers for the second!44 bytes:
[~>0\:2%\#,2*1-tr[...rep]flatmap,$sumonpref]... and then I remembered I had a builtin for mapping prefixes. ^-^
fuente
Julia 0.6, 44 bytes
Try it online!
Since OP mentions "the output format is flexible", this prints an array of sub arrays, eg. U(3) =>
[[0, 1], [0, -1, -2, -1], [0, 1, 2, 3]].i%2*2-1decides the sign of the current subarray - negative for even numbers, positive for odd.[0:i;(n>i)*~-i:-1:1]is in two parts. 0:i is straightforward, the range of values from 0 to the current i. In the next part, ~-i:-1:1 is the descending range from i-1 to 1. But we want to append this only if we're not yet at the final value, so multiply the upper end of the range by (n>i) so that when n==i, the range will be 0:-1:1 which ends up empty (so the array stops at n correctly).And here's a version that can support random access - the inner lambda here returns the i'th term of the sequence without having to have stored any of the terms before it. This one gives the output as a single neat array too.
4947 bytesTry it online!
fuente