Tantangan:
Diberikan input bilangan bulat positif n , buat vektor yang mengikuti pola ini:
0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n
Atau, dijelaskan dengan kata-kata: Vektor dimulai pada 0
, dan membuat peningkatan 1
hingga mencapai bilangan bulat positif terkecil terkecil yang bukan bagian dari urutan, kemudian membuat penurunan hingga mencapai bilangan bulat terkecil (dalam besaran) bahkan bilangan negatif yang tidak adalah bagian dari urutan. Terus seperti ini sampai n
tercapai. Urutannya akan berakhir pada positif n
jika n
ganjil, dan negatif n
jika n
genap.
Format output fleksibel.
Kasus uji:
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
Anda dapat memilih untuk mengambil n -diindeks. n = 1
akan memberi 0 1 0 -1 -2
.
Ini adalah kode-golf , jadi kode terpendek dalam setiap bahasa menang! Penjelasan didorong seperti biasa!
Jawaban:
R ,
5854504843 byte-2 byte terima kasih kepada MickyT
Cobalah online!
sumber
Perl 6 ,
6026 byteCobalah
Cobalah
Diperluas:
(-1,-*...*)Z*0..$_
menghasilkan urutan0 1 -2 3 -4 5
sumber
Python 2 ,
695756 byteCobalah online!
Untuk setiap
n
sampaiinput
denganrange(-n,n)
(inklusif) dihitung, terbalik saatn
ini jumlah yang lebih, memiliki tinju dua angka (setelah inversi) dihapus, dan kemudian ditambahkan ke output.sumber
05AB1E ,
97 byteDisimpan 2 byte berkat @Emigna
Cobalah online!
Jawaban 05AB1E pertama saya (saya pikir), jadi saya mungkin kehilangan beberapa trik ...
Penjelasan
Saya harus berterima kasih kepada @ Dennis untuk penggunaan asli
Ÿ
, kalau tidak sayamungkin tidakakan pernah tahu tentang itu ...sumber
ÝεDÈi®*}}Ÿ
tanpa memeriksa,ā®sm
gila pintar haha.05AB1E ,
1514 byteCobalah online!
Penjelasan
sumber
JavaScript (ES6), 56 byte
Cobalah online!
Berkomentar
sumber
Haskell , 43 byte
Cobalah online!
Menghitung jumlah kumulatif yang dinegasikan dari daftar
[(-1)^k|k<-[1..n],_<-[2..2*k]]
, yang merupakann
baris pertama darisumber
Jelly ,
119 byteCobalah online!
Bagaimana itu bekerja
sumber
Haskell ,
4842 byteCobalah online!
Berkat Οurous untuk -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:sumber
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!
sumber
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!
sumber
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!
sumber
Java 8,
858379 bytes-6 bytes thanks to @OlivierGrégoire.
Try it online.
Explanation:
sumber
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!
sumber
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 +
sumber
+\0,¯1*⍳(/⍨)1+2×⍳
is 17Haskell, 47 bytes
Try it online!
sumber
Java (JDK 10), 98 bytes
Try it online!
sumber
MATL,
1715 bytes-2 bytes thanks to Luis Mendo!
Try it online!
Explanation for
n=3
:sumber
Octave,
444241 bytes2 bytes removed thanks to @StewieGriffin, and 1 byte further removed thanks to @Giuseppe!
Try it online!
sumber
Ruby,
5247 bytesTry it online!
Below is the original 52-byte version with an explanation:
Try it online!
Walkthrough
sumber
map(&:-@)
portion?-r
.Prolog (SWI), 113 bytes
Try it online!
sumber
Python 3, 83 bytes
sumber
Husk,
1817 bytesTry it online!
sumber
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.
sumber
Jelly,
1112 bytesBah, I thought I had 11 wih
_2+ỊrN)N;¥/
Try it online!
How?
sumber
Stax, 10 bytes
Run and debug it
sumber
Scala, 119 Bytes
Ungolfed:
This can probably be golfed much better, but I wanted a solution utilizing lazy Streams.
sumber
APL (Dyalog Unicode),
3432 bytesTry it online!
Requires
⎕IO←0
-2 bytes thanks to @FrownyFrog
sumber
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. ^-^
sumber
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!
sumber