17
\$\begingroup\$

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.

\$\endgroup\$
16
  • 45
    \$\begingroup\$ @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. \$\endgroup\$
    – lynn
    Commented Aug 21, 2017 at 10:52
  • 13
    \$\begingroup\$ @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. \$\endgroup\$
    – Mayube
    Commented Aug 21, 2017 at 11:57
  • 5
    \$\begingroup\$ @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. \$\endgroup\$
    – Mr. Xcoder
    Commented Aug 21, 2017 at 12:42
  • 8
    \$\begingroup\$ 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? \$\endgroup\$ Commented Aug 22, 2017 at 7:14
  • 9
    \$\begingroup\$ 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. \$\endgroup\$
    – JAD
    Commented Aug 23, 2017 at 6:34

105 Answers 105

2
\$\begingroup\$

Swift 3, 29 bytes

{n in(0..<n).map{_ in n}}

Try it here!

Swift 3, 30 bytes

{Array(repeating:$0,count:$0)}

Try it here!

\$\endgroup\$
2
  • \$\begingroup\$ I wonder if we can get the input as the same variable repeated twice. Then you could have a 10 byte solution: 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)) \$\endgroup\$
    – Alexander
    Commented Sep 21, 2017 at 6:41
  • \$\begingroup\$ @Alexander No, we cannot get the input twice. \$\endgroup\$
    – Mr. Xcoder
    Commented Sep 21, 2017 at 6:52
2
\$\begingroup\$

Common Lisp, 31 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 6, 9 bytes

{$_ xx$_}

Try it online

Uses the built-in list/statement repeat operator xx

\$\endgroup\$
2
\$\begingroup\$

Kotlin, 22 bytes

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

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$
    – DJMcMayhem
    Commented Aug 21, 2017 at 16:40
2
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Use Try It Online: tio.run/#php \$\endgroup\$
    – Nnnes
    Commented Aug 21, 2017 at 12:18
  • \$\begingroup\$ @Nnnes it's better like this ? \$\endgroup\$
    – Frankich
    Commented Aug 21, 2017 at 12:28
  • 2
    \$\begingroup\$ 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 :/ \$\endgroup\$
    – Christoph
    Commented Aug 22, 2017 at 6:24
  • \$\begingroup\$ function a($b){for($n=$b;$b--;)$a[]=$n;return$a;} would be valid for example. \$\endgroup\$
    – Christoph
    Commented Aug 22, 2017 at 6:29
  • \$\begingroup\$ ... or for($b=$argn;$b--;)echo$argn,_; (and don´t forget to count +1 for the R flag) \$\endgroup\$
    – Titus
    Commented Aug 22, 2017 at 6:34
2
\$\begingroup\$

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]
\$\endgroup\$
2
  • \$\begingroup\$ Can you call this with 5 | % {@($_)*$_}? If so you don't need the 3 bytes ($f=) required to name the function. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 22, 2017 at 19:21
  • \$\begingroup\$ Yes it can. Thanks! \$\endgroup\$ Commented Aug 22, 2017 at 19:32
2
\$\begingroup\$

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.
\$\endgroup\$
5
  • \$\begingroup\$ Ans+0rand(Ans for 5 bytes \$\endgroup\$
    – Oki
    Commented Aug 25, 2017 at 21:38
  • \$\begingroup\$ Thanks, I'll post that in another solution, since it's pretty different! \$\endgroup\$
    – Adalynn
    Commented Aug 25, 2017 at 22:51
  • \$\begingroup\$ Actually, that's six bytes, rand( isn't a thing. It's Ans + 0 rand ( Ans. \$\endgroup\$
    – Adalynn
    Commented Aug 25, 2017 at 23:05
  • \$\begingroup\$ You're right. Instincts.. \$\endgroup\$
    – Oki
    Commented Aug 25, 2017 at 23:11
  • \$\begingroup\$ Also should mention that on newer calculators (CSE?) you can use ceiling() to save a byte \$\endgroup\$
    – Oki
    Commented Aug 25, 2017 at 23:20
2
\$\begingroup\$

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!

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

TI Basic, 6 bytes

Ans+0rand(Ans

Thanks to @Oki for this solution!

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$
    – Oki
    Commented Aug 26, 2017 at 11:14
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Flurry, 12 bytes

({}){({})}{}

Run example

$ ./flurry -inn -c "({}){({})}{}" 3
3 3 3
$ ./flurry -inn -c "({}){({})}{}" 6
6 6 6 6 6 6

This uses the "push x y times" construct y {({})} x, just that in this case both are the input number n.

({})  Pop and push n; evaluates to n
{({})}  A lambda that returns its argument unchanged,
        pushing its argument once onto the stack
{}    Pop n
\$\endgroup\$
1
\$\begingroup\$

Pyke, 3 bytes

]1*

Try it here!

]1  - Wrap the input into a list.
  * - Repeat the list.
\$\endgroup\$
1
\$\begingroup\$

J, 2 bytes

#~

Try it online!

# copy

~ self

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

QBasic 4.5, 48 bytes

INPUT a
DIM t(a)
FOR x=1TO a
t(x)=a
NEXT
\$\endgroup\$
1
\$\begingroup\$

Aceto, 3 bytes

Assumes the integer is on the stack already.

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

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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:

(({})){({}<(({}))>[()])}{}{}
\$\endgroup\$
1
1
\$\begingroup\$

Gaia, 2 bytes

Try it online!

w   Wrap input into list
 ×  Repeat (input) times
\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

Prolog (SWI), 33 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

MacOS bash, 12

jot $1 $1 $1

This also works on Linux if the jot package is installed, e.g. sudo apt install athena-jot.

Try it online.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ this can only handle single-digit numbers properly \$\endgroup\$ Commented Aug 23, 2017 at 11:23
  • \$\begingroup\$ @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. \$\endgroup\$
    – Uriel
    Commented Aug 23, 2017 at 13:55
1
\$\begingroup\$

Clojure, 13 bytes

#(repeat % %)
\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.