Défi:
Avec une entrée entière positive n , créez un vecteur qui suit ce modèle:
0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n
Ou, expliqué avec des mots: le vecteur commence à 0
, et incrémente 1
jusqu'à atteindre le plus petit entier positif impair ne faisant pas partie de la séquence, puis il décrémente jusqu'à ce qu'il atteigne le plus petit (même en entier) entier négatif qui n'est pas ne fait pas partie de la séquence. Il continue ainsi jusqu'à ce quen
soit atteint. La séquence se terminera sur positif n
si n
est impair et négatif n
si n
est pair.
Le format de sortie est flexible.
Cas de test:
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
Vous pouvez choisir de prendre le n indexé à zéro. n = 1
donnerait alors 0 1 0 -1 -2
.
C'est du code-golf , donc le code le plus court dans chaque langue gagne! Les explications sont encouragées comme toujours!
Réponses:
R,
5854504843 bytes-2 bytes thanks to MickyT
Try it online!
la source
Perl 6,
6026 bytesTry it
Try it
Expanded:
(-1,-*...*)Z*0..$_
generates the sequence0 1 -2 3 -4 5
la source
Python 2,
695756 bytesTry it online!
For each
n
up to theinput
therange(-n,n)
(inclusive) is calculated, inverted whenn
is an even number, has the fist two numbers (after the inversion) removed, and then appended to the output.la source
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...la source
ÝεDÈi®*}}Ÿ
without checking,ā®sm
is crazy smart haha.05AB1E,
1514 bytesTry it online!
Explanation
la source
JavaScript (ES6), 56 bytes
Try it online!
Commented
la source
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 firstn
rows ofla source
Jelly,
119 bytesTry it online!
How it works
la source
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*x
which isx
wheni
is even and-x
wheni
is odd. Previous iterations where:la source
1-i
instead of-i+1
in 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!
la source
using
statements 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)*2
is just2*n-2
and 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>0
instead of `!=0, saves a byte.static int[] f(int n)
can becomef=n=>
by using a (recursive) lambda, and(n-1)*2
can become~-n*2
to 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 thei
th 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!
la source
n
version? 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!
la source
Java 8,
858379 bytes-6 bytes thanks to @OlivierGrégoire.
Try it online.
Explanation:
la source
j
).i
go 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
approx
is less golfy.n=n^2+1
can be replaced by,,n^2+1
; or by0:n^2+1
(positional argumentxout
) for the same byte count :R, 56 bytes
Try it online!
la source
approx
will work here in a similar manner to Luis Mendo's Octave solution as well.diffinv
andapprox
from 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
+\⍣¯1
denotes 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 +
la source
+\0,¯1*⍳(/⍨)1+2×⍳
is 17Haskell, 47 bytes
Try it online!
la source
Java (JDK 10), 98 bytes
Try it online!
la source
MATL,
1715 bytes-2 bytes thanks to Luis Mendo!
Try it online!
Explanation for
n=3
:la source
Octave,
444241 bytes2 bytes removed thanks to @StewieGriffin, and 1 byte further removed thanks to @Giuseppe!
Try it online!
la source
Ruby,
5247 bytesTry it online!
Below is the original 52-byte version with an explanation:
Try it online!
Walkthrough
la source
map(&:-@)
portion?-r
.Prolog (SWI), 113 bytes
Try it online!
la source
Python 3, 83 bytes
la source
Husk,
1817 bytesTry it online!
la source
Charcoal, 19 bytes
Try it online! Link is to verbose version of code. Explanation:
Alternative explanation:
Loop over the integers from
0
to 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.
la source
Jelly,
1112 bytesBah, I thought I had 11 wih
_2+ỊrN)N;¥/
Try it online!
How?
la source
Stax, 10 bytes
Run and debug it
la source
Scala, 119 Bytes
Ungolfed:
This can probably be golfed much better, but I wanted a solution utilizing lazy Streams.
la source
APL (Dyalog Unicode),
3432 bytesTry it online!
Requires
⎕IO←0
-2 bytes thanks to @FrownyFrog
la source
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#behead
does.)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
) over1
and-1
repeated 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
2
then subtracting1
. This gives alternating1
s and-1
s 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. ^-^
la source
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-1
decides 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!
la source