Diberikan bilangan bulat n
sebagai input, kembalikan daftar yang berisi n
, n
waktu yang diulang . Misalnya, program akan mengambil 5
dan mengubahnya menjadi[5,5,5,5,5]
. Elemen-elemen harus bilangan bulat, bukan string. Tidak ada fungsi bawaan yang menyelesaikan tugas diizinkan.
Ini adalah kode-golf , jadi aturan standar berlaku.
code-golf
number
array-manipulation
integer
Bruno E
sumber
sumber
built-in
.*
operator Python baik- baik saja? Apa contoh built-in yang tidak ok?Jawaban:
Jelly , 1 byte
Cobalah online!
Perhatikan bahwa ini bukan
n
n
built-in "repeat times" - fungsinya lebih umum dari itu. Misalnya4,5,6x1,2,3
sama dengan[4, 5, 5, 6, 6, 6]
. Diberi hanya satu argumen, Jelly kebetulan menggunakannya sebagai argumen kiri dan kanan untuk tautan yang disediakan, tetapi fungsi ini tidak melekat padax
.Jika ini tidak masuk hitungan, ada berbagai alternatif 2 byte yang menyenangkan:
dll.
sumber
x
melakukan "semua pekerjaan", tetapi tentu saja tidak - ada "0 byte" implisit dari tautan-parsing dan array-paksaan logika untuk mengubah ini menjadirepeat([n], n)
, yang merupakan jawaban lain melakukan.ṁ
danẋ
jadi jawaban ini bisa salah satu dari ketiganya. Tidak ada 3 built-in (orang akan berharap) untuk "repeatn
n
times", jadi mereka tidak bisa SEMUA menjadi "built-in" untuk itu.Python 3, 14 bytes
Try it online!
sumber
[n]*n
?Operasi Flashpoint scripting language,
5046 bytesCall with:
Keluaran:
sumber
i--
,, dan+=
dalam hal ini?APL (Dyalog), 2 bytes
Five equally short solutions. Last two are courtesy of Zacharý.
Try it online!
⍴
cyclically reshape⍨
selfTry it online!
/
replicate⍨
selfTry it online!
\
expand⍨
selfTry it online!
⌿
replicate along first (and only) axis⍨
self⍀
expand along first (and only) axis⍨
selfTry it online!
sumber
⌿⍨
and⍀⍨
work.⍨
by typing ``selfie.Mathematica, 10 bytes
sumber
Proton, 8 bytes
Try it online!
sumber
Octave, 12 bytes
Try it online!
sumber
~
is thenot
operator that converts 1:n to an array of0
s of size n. You can use!
instead of it.JavaScript (ES6), 19 bytes
Try it
sumber
...
- whatever next?!Pyth, 2 bytes
Test suite.
sumber
Haskell, 13 bytes
Try it online! Usage:
f 5
yields[5,5,5,5,5]
. Forn=5
,[1..n]
yields the list[1,2,3,4,5]
.n<$
replaces each element of this list withn
.sumber
join replicate
join
is not part of Prelude and thus requiers a lengthyimport Control.Monad
, which rarely makes it useful for golfing.R, 18 bytes
Try it online!
sumber
rep(n<-scan(),n)
too close to a builtin?05AB1E, 2 bytes
Try it online!
sumber
.D)
.Dodos, 76 bytes
Try it online!
Explanation:
f
is an alias fordab
(tail).s
is subtraction, as explained on the wiki: (x, y) → (0, y−x) when x ≤ y.t
maps (a, b, c…) to (b+c+…, a+b+c+…).f s t
maps (a, b, c…) to a. This is our “head” function.d
dips only the head of its argument: (a, b, c…) → (|a−1|, b, c…)r
is the main repetition logic. We map (a, b) to (*r(|a−1|, b), b).For example, r(4, 7) will evaluate as
Finally, we define
2
, which maps n → (n, n), and definemain
asf f r 2
, computing r(n, n) and chopping off the first two elements.sumber
Japt, 2 bytes
Test it
Explanation
Implicit input of integer
U
. Generate an array of integers from0
toU-1
. Fill it withU
. Implicit output of resulting array.sumber
TeX, 81 bytes
Usage
sumber
Husk, 2 bytes
Try it online!
Polite alternative (3 bytes)
Try it online!
sumber
Haskell (14 bytes)
Thanks to @nimi, I don't need any import anymore. Yay!
It's a function that takes an integer argument; for example, the following returns
[5,5,5,5,5]
:sumber
id=<<replicate
? It's also 14 bytes but doesn't need the import.Java (OpenJDK 8),
5048 bytesTry it online!
-2 bytes thanks to @Jakob
Inspired by the comments in @OlivierGrégoire's post, and optimized a little further. Takes an integer input, creates an IntStream of
n
elements, then maps each element ton
and returns it.sumber
java.util.Arrays.stream(new int[n])
.Perl 5,
1814 bytes-4 bytes thanks to @DomHastings
Try it online!
Is
x
a builtin that does the entire task? Sort of? Not really? Rules unclear?Edit: Yeah, probably it's fine.
sumber
$_[0]
to@_
! Also the second can be"@_"
I think...$_=$_ x$_
withperl -pe
?x
does string repetition, not list repetition, unless the left operand is in parentheses (or is aqw
operator) and thex
is evaluated in list context. And of course$_
is a scalar, not a list.J, 2 bytes
Same as the APL answer: reflexively shape the input. In other words:
sumber
Brainbash, 39 bytes
Try it online!
Prints
N
N
times. Works by generating 32, taking input, then duplicating the input twice, then output the first for each 1 in the second.sumber
C (gcc), 55 bytes
Try it online!
Returns a list of
k
integers.sumber
eax
for locals. Go figure.*f(k){int r[k],
instead ofint*f(k){int*r=malloc(k*4),
Röda, 10 bytes
Try it online!
Explanation:
sumber
[_]*_
=[_1]*_2
. Because the first underscore is the first, it has automatically the number 1.Groovy, 9 bytes
Try it online!
Perhaps the most competitive groovy answer I've done to date.
sumber
brainfuck, 16 bytes
Try it online!
The breakdown:
As I'm sure you're aware, brainfuck takes input and output values as ASCII characters. So a
!
is represented as the value 33.sumber
Coreutils, sed, 14 bytes
As a zsh function,
2019 bytes:Try it online!
sumber
yes $1|sed $1q
?declare -i
integer variables. But it also has to be an array. I'm not sure bash even supports an integer array (likeeval declare -ia "$1"
to use the first function arg as the name of an array return value.) I upvoted this because it follows the spirit of the question; I doubt the question meant to exclude languages that don't really have integer lists / arrays.MATL,
43 bytesTry it online!
Explanation:
sumber
Java (OpenJDK 8),
5856 bytesTry it online!
-2 bytes thanks to @KevinCruijssen
sumber
n->{int a[]=new int[n],i=n;for(;i-->0;)a[i]=n;return a;}
IntStream.generate(() -> n).limit(n)
but decided it wasn't worth typing up and upvoted this instead :)cQuents v0, 3 bytes
Try it online!
Explanation
sumber
&
instead of::
Swift 3, 29 bytes
Try it here!
Swift 3, 30 bytes
Try it here!
sumber
Array.init
. Just assign it to a variable with a type alias, and boom:let f: (Int, Int) -> [Int] = Array.init; print(f(5, 5))