Turn an integer n into a list containing it n times

Given an integer n as input, return a list containing n, repeated n times. For example, the program would take 5 and turn it into [5,5,5,5,5]. The elements need to be integers, not strings. No built-in functions that accomplish the task are allowed.

This is , so standard rules apply.

• @BrunoE That still doesn’t answer the why. Do you have an actual reason to ban built-ins? To quote xnor: In general, if your challenge is too simple to be interesting, it won't be saved by banning things. – Lynn Aug 21 '17 at 10:52
• @BrunoE while I agree with the sentiment, we prefer objectivity here at ppcg. Either something is or is not valid, opinions shouldn't come into the equation. – Skidsdev Aug 21 '17 at 11:57
• @BrunoE The specs must be made such that one can indisputably decide whether an entry is valid or not. Please share your thoughts on the existing answers and make the specs more objective when it comes to built-in. – Mr. Xcoder Aug 21 '17 at 12:42
• I'm still confused what counts as a "built-in" for this task. Is Python's * operator ok? What's an example of a built-in that isn't ok? – Steve Bennett Aug 22 '17 at 7:14
• I am suprised nobody has mentioned this yet, but we have a sandbox for challenges where you can post them to get feedback on them, before they go live. This way you might've prevented discussing the rules of the challenge while others already submitted their answers. – JAD Aug 23 '17 at 6:34

Common Lisp, 31 bytes

(lambda(x)(fill(make-list x)x))


Try it online!

Perl 6, 9 bytes

{$_ xx$_}


Try it online

Uses the built-in list/statement repeat operator xx

Kotlin, 22 bytes

{n:Int->(1..n).map{n}}


Try it online!

• Welcome to the site! :) – DJMcMayhem Aug 21 '17 at 16:40

Tampio, 109 85 bytes

a:n uni on a a:na
nolla a:na on tyhjyys
a:n seuraaja o:na on o lisättynä a:han o:na


Explanation:

a:n uni on   a a:na
uni(a)  =  f(a,a)

nolla a:na on tyhjyys
f(0,    a)   =  []

a:n seuraaja o:na on o lisättynä a:han o:na
f(a   +1,      o)   =  o :       f(a,    o)

uni(a)   = f(a,a)
f(0  ,a) = []
f(a+1,o) = o : f(a,o)


PHP, 19 Bytes

I'm new here can anyone tell if this is ok ?

for(;$b--;)$a[]=$n;  try it online! EDIT: looks to not be a valid form so : 32 Bytes (by Titus) (+1 Byte for R flag) for($b=$argn;$b--;)echo$argn,_;  try it online! • Use Try It Online: tio.run/#php – Nnnes Aug 21 '17 at 12:18 • @Nnnes it's better like this ? – Frankich Aug 21 '17 at 12:28 • Wellcome to PPGC. You need to use some form of input and output you're not allowed to handle that outside. Use e.g.$_GET, $argv,$argn or read from stdin. for($a=$argn;$argn--;)$b[]=$a;echo implode(',',$b); would work. echo$a=$argn;for(;--$argn;)echo',',$a; would be better though ;) You would be allowed to submit a function but they are usally longer in php :/ – Christoph Aug 22 '17 at 6:24
• function a($b){for($n=$b;$b--;)$a[]=$n;return$a;} would be valid for example. – Christoph Aug 22 '17 at 6:29 • ... or for($b=$argn;$b--;)echo$argn,_; (and don´t forget to count +1 for the R flag) – Titus Aug 22 '17 at 6:34 Ruby, 10 characters ->n{[n]*n}  Sample run: irb(main):001:0> ->n{[n]*n}[5] => [5, 5, 5, 5, 5]  Try it online! Ruby, 18 characters ->n{Array.new n,n}  Sample run: irb(main):001:0> ->n{Array.new n,n}[5] => [5, 5, 5, 5, 5]  Try it online! PowerShell, 13 10 bytes {@($_)*$_}  Usage PS C:\> 2 | % {@($_)*$_} 2 2  And: PS C:\> 5 | % {@($_)*$_} 5 5 5 5 5  By default, PowerShell shows array content one element per line. Running it through the ConvertTo-Json cmdlet shows it in a more familiar form: PS C:\> 5 | % {@($_)*$_} | ConvertTo-Json -Compress [5,5,5,5,5]  • Can you call this with 5 | % {@($_)*$_}? If so you don't need the 3 bytes ($f=) required to name the function. – Wheat Wizard Aug 22 '17 at 19:21
• Yes it can. Thanks! – Don Cruickshank Aug 22 '17 at 19:32

TI Basic, 10 bytes

This assumes an empty list

Ans→dim(L₁:L₁+Ans


Here's the hex, along with the code to explain the byte count:

72  04 B5   5D 00 3E 5D 00 70 71
Ans →  dim( L₁    :  L₁    +  Ans


How?

Ans→dim(L₁:L₁+Ans
Ans→dim(L₁        Since the list is empty, this makes L₁ a list on Ans 0's
:       Separator
L₁+Ans Since TI-Basic vectorizes some commands,
this adds Ans (basically the input here) to each element in L₁,
which results in Ans for each element.

• Ans+0rand(Ans for 5 bytes – Oki Aug 25 '17 at 21:38
• Thanks, I'll post that in another solution, since it's pretty different! – Zacharý Aug 25 '17 at 22:51
• Actually, that's six bytes, rand( isn't a thing. It's Ans + 0 rand ( Ans. – Zacharý Aug 25 '17 at 23:05
• You're right. Instincts.. – Oki Aug 25 '17 at 23:11
• Also should mention that on newer calculators (CSE?) you can use ceiling() to save a byte – Oki Aug 25 '17 at 23:20

C# (Mono), 38 37 bytes

n=>System.Linq.Enumerable.Repeat(n,n)


Old version for 38 bytes:

n=>"".PadLeft(n,(char)n).ToCharArray()


Try it online!

A char in C# is an int underneath so this appears to be valid.

C# (Mono), 44 bytes

using System.Linq;n=>new int[n].Select(_=>n)


If the above is invalid this is for an extra 6 bytes.

Try it online!

• Also 38 Bytes but more readable: n=>new String((char)n,n).ToCharArray() – TheJoeIaut Aug 21 '17 at 16:20
• @TheJoeIaut So you're aware that would have to be new string... note the lower case :) Though yes it is the same length unfortunately. – TheLethalCoder Aug 21 '17 at 16:22
• 25: n=>Enumerable.Repeat(n,n) – Manfred Radlwimmer Aug 24 '17 at 10:08
• @ManfredRadlwimmer Actually 37 but does save a byte thanks! – TheLethalCoder Aug 24 '17 at 10:10
• Oh, right - the using – Manfred Radlwimmer Aug 24 '17 at 10:10

TI Basic, 6 bytes

Ans+0rand(Ans


Thanks to @Oki for this solution!

• 0 can be replaced with lots of things (int(/iPart(, imag(, angle(, ...). Alternative solution: Ansnot(not(rand(Ans. Other noteworthy solutions (1 byte longer) randInt(Ans,Ans,Ans, Ansrand(Ans)^0, Ans(1^rand(Ans, -Ansint(-rand(Ans – Oki Aug 26 '17 at 11:14

Element, 9 bytes

_2:'[2:]


Try it online!

_        input
2:       double the input
'        push one to control stack
[2:]    for (number), double it, output one of them


Pyke, 3 bytes

]1*


Try it here!

]1  - Wrap the input into a list.
* - Repeat the list.


J, 2 bytes

#~


Try it online!

# copy

~ self

Ohm, 4 bytes

DÄWî


Try it online!

DÄWî

D     duplicate
Ä    push n onto the stack n times
W   wrap the stack in a list
î  cast to integer


Pari/GP, 16 bytes

n->[n|x<-[1..n]]


Try it online!

Or:

n->vector(n,x,n)


Try it online!

Or:

n->powers(1,n,n)


Try it online!

Stacked, 6 bytes

[:rep]


Try it online!

This is an anonymous function that takes input on the stack and leaves output on the stack. This simply takes the input, duplicates it, then repeats the input by the input.

QBasic 4.5, 48 bytes

INPUT a
DIM t(a)
FOR x=1TO a
t(x)=a
NEXT


Aceto, 3 bytes

Assumes the integer is on the stack already.

d×


This just duplicates the top stack element and then performs stack multiplication (i.e. Python's [x]*y).

SOGL V0.12, 2 bytes

∙r


Try it Here!
Outputs to the stack.
Explanation:

∙   multiply POP (string, defaults to input) by POP (number, defaults to the same input)
vertically - so to an array of strings
r  swap types - convert the strings to numbers


Brain-Flak, 28, 22 bytes

{(({}[()])<>[]())<>}<>


Try it online!

Normally, the version that uses one stack would be shorter. However, in this case it's 6 bytes longer:

(({})){({}<(({}))>[()])}{}{}


Gaia, 2 bytes

w×


Try it online!

w   Wrap input into list
×  Repeat (input) times


MY, 6 bytes

ωi×ω+↵


Try it online!

How?

• ω, push the first command line argument evaluated
• i, pop n; push [1...n]
• ×, pop a; pop b; push b*a. 0 is popped when there is nothing on the stack. Since commands vectorize ("vecify" in MY), it ends up being an array of n 0's.
• ω, push the first command line argument evaluated
• +, pop a; pop b; push b+a. Adds the command line argument to every item in the array of 0 repeated n times, giving us n repeated n times
• ↵, pop n; print n with a newline.

Prolog (SWI), 33 bytes

X*L:-maplist(=(X),L),length(L,X).


Try it online!

jot $1$1 $1  This also works on Linux if the jot package is installed, e.g. sudo apt install athena-jot. Actually, 2 bytes ;α  Explanation: ;α ; duplicate the input α push a list containing n copies of n  α is not a builtin for this challenge by the usual definition, because it does not solve this challenge by itself. α takes the top stack element (call it a) and repeats the next stack element (b) a times. It's roughly equivalent to iterable multiplication in Python ([5, 1]*2 == [5, 5, 1, 1]), but also works on non-iterables (52α yields [5, 5]). Try it online! 4, 32 bytes 3.700001000260201801500101010294  Try it online! If you have concerns regarding the input format, please read this first. Transpiles roughly as grid[1] = grid[0] = input() while grid[1] != 0: print(grid[0]) grid[1] -= 1  • this can only handle single-digit numbers properly – Timothy Groote Aug 23 '17 at 11:23 • @TimothyGroote no; read the meta post. this handles numbers as bytes, because that's the main form of input for that language. input 1 is 48, and will print 1 (byte representation) 48 times. – Uriel Aug 23 '17 at 13:55 Braingolf, 6 bytes !A.$_=


Haven't had Dennis pull this version to TIO yet, but A used to be useless, it basically acted the same as & in most cases. Now it pops the top of the stack and repeats the next operator that many times.

Explanation

!A.$_= Implicit input from commandline args !A Read top of stack without popping, run next operator that many times . Duplicate top of stack$_   Pop top of stack
=  Print stack


We have to pop the top of the stack because otherwise we'll have 1 too many copies of n. If we remove the ! and let A pop the top of the stack, duplicate will have nothing to duplicate.

Clojure, 13 bytes

#(repeat % %)


Coq, 68 bytes

fun n=>(fix f k n:=match n with|0=>nil|S n=>(k::f k n)%list end) n n


Of course there's a library function, giving 46 bytes:

Require List. Definition f n:=List.repeat n n.


Recursiva, 4 bytes

*aAa


Try it online!

Explanation:

*a  : Multiply a times(Here, 8) [8,8,8,8,8,8,8,8]
A : List-ify a [8]
a: All a's will be assigned with the first input argument) 8