# 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

# Excel VBA, 49 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and outputs a array of range values, o, with bounds (1 To [A1], 1) to the VBE immediate window.

[A1].Resize([A1],1)=[A1]:o=Sheet1.UsedRange.Value

• Imo, if your language has no lists, use the next closest thing. – JAD Aug 23 '17 at 6:35

# SNOBOL4 (CSNOBOL4), 41 bytes

R	R =ARRAY(N,N)	:(RETURN)
DEFINE('R(N)')


Try it online!

ARRAY initializes an array with the given indices, in this case, 1:N. It has an optional argument which it uses to initialize all the values in the array.

# ///, 23 bytes

/i///0./.1//1/i,//.//i.


Try it online!

Since there is no other way to take input in ///, it is hard coded. Also, since /// is a string-based language, input is in unary.

Example with input 5:

/i/00000//0./.1//1/i,//.//i.


How it works (with example input 5):

/i/00000//0./.1//1/i,//.//i.    Program with input.
/i/00000/                       Replace i with input. This is to make 1 uniform input.
/0./.1/                Replace "0." with ".1". This turns all the 0s to 1s in the later i without bothering the next piece of code.
/1/i,/          Replace 1s with input and a comma. This places the input into where the 1s were, essentially replacing each unary char of the input with the input, delimited with commas.
/.//      Remove the period.
i.    Input with period, main thing being modified by code.


# Python 3, 63 54 Bytes

Saved 9 bytes thanks to Xcoder

a=[];i=input()
for _ in" "*int(i):a+=(i);
print(a)


Same code but will print every iteration of the list, saves 2 bytes

a=[];i=input()
for _ in" "*int(i):a+=(i);print(a)

• Thanks, although I am still trying to figure out how I could get rid of the variables I needed to set – Mercury Platinum Jan 31 '18 at 18:18
• You should post that as your answer, not going to copy it. – Mercury Platinum Jan 31 '18 at 18:26
• That's up to you :) However I already have my own answer. – Mr. Xcoder Jan 31 '18 at 18:28

# Fortran (GFortran), 45 bytes

SUBROUTINE B(I)
INTEGER A(I)
A=I
PRINT*,A
END


Try it online!

This is a simple version, in which the subroutine is used to avoid the allocation stuff, since we have a unknown length array. The TIO link leads to a version with the full program, with 67 bytes in total.

# K (oK), 4 bytes

Solution:

x#x:


Try it online!

Explanation:

5#5 means take from 5, 5 times. But doesn't appear we can do the same trick as APL/J to reduce this one down.

x#x: / the solution
x: / save input as x
x#   / take from it x times


# Stax, 3 bytes

]x*


Run and debug it

Internally it actually converts an integer to the array required but the implicit output changes it to codepoints. By default, output by codepoint is allowed though.

## Explanation

]      Convert input to singleton
x*    Repeat input times

• y* does it without using the codepoint exemption. y is the first line of standard input by default. – recursive Mar 26 '18 at 23:06
• True, but "The elements need to be integers, not strings". – Weijun Zhou Mar 27 '18 at 1:21

# 12-basic, 17 14 bytes

?[X=INPUT()]*X


Try

# Excel VBA, 26 Bytes

Anonymous VBE immediate window function that takes input, n from range [A1] and returns an array object X with n stuffed into it n times.

x=Split([Rept(A1&" ",A1)])


# Japt-m, 1 byte

N


Try it online!

-m is a flag that runs the program on each item in the input. Because the input is a number, it gets turned into a range first. N is the input.

# Gol><>, 7 bytes

I:R:~D;


This uses the convenient 'D' - Debug operator, which outputs the stack!

I       //Get integer input
:R:    //Repeat input, double the number
~D; //Delete the last item in the stack(to get the right number) and output the entire stack with debug


Try it online!

• lol, there's already a 5 byte one – ASCII-only Feb 22 '19 at 11:25
• @ASCII-only I didn't see that... Well, at the very least mine doesn't use a header, it does it on its own – KrystosTheOverlord Feb 22 '19 at 22:06
• Yeah well that's just the difference between a function and a program :P – ASCII-only Feb 22 '19 at 22:53

# Kotlin, 2019 15 bytes

Shorter Kotlin answer using the List constructor.

{n->List(n){n}}


Try it online!

Ungolfed:

// lambda function
{ n ->
List(n) { n }  // create a List of n Ints where each item is n
}


Because List is actually an interface, List() is actually a function that creates a List of the specified type (here figured out through inference) and a fixed size, using the given lambda to initialize the value of each item. { n } means the lambda will always return the value of n.

• 15 – ASCII-only Feb 22 '19 at 11:19

# Rockstar, 83 bytes

Rockstar only has string I/O so output is a comma delimited string, which may not be allowed by the spec. To conform with the spec, though, the second line ensures the elements in the actual array are integers.

listen to N
cast N
X's0
while N-X
let L at X be N
let X be+1

join L with ","
say L


Try it here (Code will need to be pasted in)