# 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

Just found this lang and wanted to give it a go.

# LOLCODE, 164 bytes

HAI 1.2
I HAS A CHZBRGR
GIMMEH CHZBRGR
CHZBRGR IS NOW A NUMBR
IM IN YR CRIB UPPIN YR HMBRGR TIL BOTH SAEM CHZBRGR AN HMBRGR
VISIBLE CHZBRGR
IM OUTTA YR CRIB
KTHXBYE


Try it online!

# Pyt, 3 bytes

Đ⑴*


Explanation:

                Implicit input
Đ               Duplicates input
⑴             Pushes an array, [1,1,...,1], with input 1's, onto the stack
*            Multiply the array element-wise by the input
Implicit output


Try it online!

# Tcl, 22 bytes

time {lappend L $n}$n


Try it online!

With built-ins:

# Tcl, 13 bytes

lrepeat $n$n


Try it online!

# FORTH 21 Bytes

: B 0 DO DUP . LOOP ;


OUTPUT:

10 B
10 B 10 10 10 10 10 10 10 10 10 10


# Gol><>, 5 bytes

:MR:B


Try it online!

### Example full program & How it works

1AGIE;GDlR~
:MR:B

1AG          Register row 1 as function G
IE;       Take number input; halt on EOF
G      Call G
D     Print the stack contents
lR~  Empty the stack
Repeat indefinitely

:M           Duplicate top and decrement
R:         Duplicate n-1 times
B        Return


# Charcoal, 954 3 bytes

Ｅθθ


Try it online!

-4 bytes thanks to Neil, -1 byte thanks to Erik the Outgolfer

## Explanation

Ｅ  Ｉθ Map with lambda returning first input cast to integer
Ｎ    Implicit range over next (first) input

• Ah, × vectorises these days, so to get list repetition, you need to embed it in a list? – Neil Aug 21 '17 at 12:14
• Yeah, not sure if it's the best idea :/ but someone said so – ASCII-only Aug 21 '17 at 12:15
• Never mind, ＮθＥθθ is shorter even with non-vectorising ×. – Neil Aug 21 '17 at 12:16
• Damnit lol I had this in mind, do you want to post a separate answer? – ASCII-only Aug 21 '17 at 12:18
• Ah, Mold still works, that's good to know! – Neil Aug 21 '17 at 12:32

# [Swift 3], 55 49 bytes

func a(n:Int)->[Int] {return (0..<n).map{_ in n}}

• $0+n-$0 can be replaced by _ in n for -1 bytes. Additionally, you can print rather than return and ->[Int] to save tons of bytes. You can also remove Array and the space before it. This ends up being 41 bytes: func a(n:Int){print((0..<n).map{_ in n})} – Mr. Xcoder Aug 21 '17 at 12:52
• Didn't realise you didn't need the Array there and interesting how the _ in n works, would never have thought of that. However, the challenge says to "return a list", which to me would suggest you need to return something, not just print it, but that may just be my interpretation. I've updated with the other suggestions though, thanks! – AnonymousReality Aug 21 '17 at 14:52

## REXX, 24 bytes

arg n
say copies(n' ',n)


Uses the built-in function copies(), and naturally, this being REXX, the copies are integers just as much as they are strings.

# WendyScript, 10 bytes

#:(x)[x]*x

(#:(x)[x]*x)(5) // => [5,5,5,5,5]


Try it online!

# V, 5 bytes

Du@"Ä


Try it online!

# Pyth, 5 bytes

Super simple

.[QQY


Explanation:

.[QQY     Pad Y with eval(input) copies of eval(input)


Try it online!

# R, 7 bytes

0*1:n+n


Try it online!

• This isn't a function nor a full program. You have to handle input and output: function(n)0*1:n+n, or cat(0*1:(n=scan())+n) – JAD Aug 23 '17 at 6:28

# Julia 12

~x=fill(x,x)


Also it kinda generalizes to tuples making tensors.

julia> ~3
3-element Array{Int64,1}:
3
3
3

julia> ~tuple(2,4)
2×4 Array{Tuple{Int64,Int64},2}:
(2, 4)  (2, 4)  (2, 4)  (2, 4)
(2, 4)  (2, 4)  (2, 4)  (2, 4)

julia> ~tuple(2,4,3)
2×4×3 Array{Tuple{Int64,Int64,Int64},3}:
[:, :, 1] =
(2, 4, 3)  (2, 4, 3)  (2, 4, 3)  (2, 4, 3)
(2, 4, 3)  (2, 4, 3)  (2, 4, 3)  (2, 4, 3)

[:, :, 2] =
(2, 4, 3)  (2, 4, 3)  (2, 4, 3)  (2, 4, 3)
(2, 4, 3)  (2, 4, 3)  (2, 4, 3)  (2, 4, 3)

[:, :, 3] =
(2, 4, 3)  (2, 4, 3)  (2, 4, 3)  (2, 4, 3)
(2, 4, 3)  (2, 4, 3)  (2, 4, 3)  (2, 4, 3)


# TI-BASIC, 12 bytes

Ans→dim(L₁:Ans(L₁+1


Assumes an empty list.

• No, Ans, →, & dim(, 1 byte, L₁, 2 bytes, :, Ans, & (, 1 byte, L₁, 2 bytes, + & 1, 1 byte. – Scott Milner Aug 23 '17 at 21:27
• Whoops, thought Ans( was all one byte, derp. – Zacharý Aug 23 '17 at 21:33

# Objective-C 107 Bytes

#define a NSMutableArray
-(a*)f:(int)n{a*m=[a arrayWithCapacity:n];for(int i=0;i<n;i++)m[i]=@(n);return m;}


A little macro abuse and a basic loop, everyone else took the good languages...

## Usage

[self f:5];


returns an array of number objects of the given value

# VBA, 75 Bytes

Defined Function that takes input n of expected type Integer and returns an array of length n filled with n to STDOUT

Function l(n)
Dim o
ReDim o(1To n)
For i=1To n
o(i)=n
Next
l=o
End Function


# Microscript II, 14 bytes

\$s{os+s}sNv*


# C++, 8656 45 bytes

Thanks to @ceilingcat for saving 30 41 bytes!

#import<list>
#define f(n)std::list<int>(n,n)


Try it online!

# Ly, 13 10 bytes

ns[>l<1-]>


Try it online!

Prints space-separated numbers. If outputting without a separator is allowed, ns[lu1-]; works for 9 bytes.

Explanation:

ns[>l<1-]>

ns         # take input and save it
[        # loop
>l<     # load save and push it to another stack
1-   # decrement
]  # end loop
> # go to other stack
# implicitly print


# F#, 32 bytes

let m n=List.map(fun x->n)[1..n]


Creates a list 1 to n, then mapping a function to each element to replace it with n.

# Axiom, 23 bytes

f(x)==[x for i in 1..x]


This 15 bytes would be better

f(x)⊳[x|i∈1..x]


f(x) return the list contains x element x but Axiom is a good approximation to that. Goes under 15 bytes it seems too much golfed

# Ruby, 12 bytes

->(n){[n]*n}


13 bytes

Array.new n,n


# QBIC, 17 bytes

dim g(:)[a|g(b)=a


This is a direct port of my QBasic answer (48 bytes), and QBIC does what it does best: compressing QBasic.

## Explanation

dim g(:) Defines an array of x elements, where x is a cmd line param
[a|      FOR b = 1 TO a (the : above creates 'a' and assigns the cmd line param to it)
g(b)=a   Set each array element to 'a'


### dc, 21

?dsn[lnpr1-d0!=F]dsFx


Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
lnp        # print register n
r          # swap the counter to the top
1-         # decrement counter
d          # duplicate counter for the comparison
0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)


Run on the command line like this:

echo 5 | dc dcscript


Output:

5
5
5
5
5


This shorter script works for n>1 (15 bytes):

? [d d z 1- <F]dsFx f


This uses the depth of the stack to determine when to stop.

• This only prints n n times, it doesn't create a list or array containing n copies of n which is what the challenge asks for, unfortunately this not a valid solution. – ბიმო Jan 30 '18 at 20:08
• @BMO: converting to a list is trivial: echo -n '['; echo 5 | dc dcscript | paste -sd, | tr -d \\n ; echo ']' – Thor Jan 30 '18 at 21:19
• That would make it a bash submission though. – ბიმო Jan 30 '18 at 22:53

# 8th, 42 37 bytes

Code

>r a:new ( r@ a:push ) r@ times rdrop


SED (Stack Effect Diagram) is: n -- a

Usage

: f >r a:new ( r@ a:push ) r@ times rdrop ;

ok> 5 f .
[5,5,5,5,5]


We could save another 6 bytes if we accept the idea of leaving numbers on r-stack after processing.

# 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.