15
\$\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\$
  • 43
    \$\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 Aug 21 '17 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\$ – Skidsdev Aug 21 '17 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 Aug 21 '17 at 12:42
  • 6
    \$\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\$ – Steve Bennett Aug 22 '17 at 7:14
  • 8
    \$\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 Aug 23 '17 at 6:34

97 Answers 97

1
\$\begingroup\$

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!

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

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!

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

Tcl, 22 bytes

time {lappend L $n} $n

Try it online!


With built-ins:

Tcl, 13 bytes

lrepeat $n $n

Try it online!

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

FORTH 21 Bytes

: B 0 DO DUP . LOOP ;

OUTPUT:

10 B
10 B 10 10 10 10 10 10 10 10 10 10 
\$\endgroup\$
1
\$\begingroup\$

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

Charcoal, 9 5 4 3 bytes

Eθθ

Try it online!

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

Explanation

E  Iθ Map with lambda returning first input cast to integer
  N    Implicit range over next (first) input
\$\endgroup\$
  • \$\begingroup\$ Ah, × vectorises these days, so to get list repetition, you need to embed it in a list? \$\endgroup\$ – Neil Aug 21 '17 at 12:14
  • \$\begingroup\$ Yeah, not sure if it's the best idea :/ but someone said so \$\endgroup\$ – ASCII-only Aug 21 '17 at 12:15
  • \$\begingroup\$ Never mind, NθEθθ is shorter even with non-vectorising ×. \$\endgroup\$ – Neil Aug 21 '17 at 12:16
  • \$\begingroup\$ Damnit lol I had this in mind, do you want to post a separate answer? \$\endgroup\$ – ASCII-only Aug 21 '17 at 12:18
  • \$\begingroup\$ Ah, Mold still works, that's good to know! \$\endgroup\$ – Neil Aug 21 '17 at 12:32
0
\$\begingroup\$

[Swift 3], 55 49 bytes

func a(n:Int)->[Int] {return (0..<n).map{_ in n}}
\$\endgroup\$
  • \$\begingroup\$ $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})} \$\endgroup\$ – Mr. Xcoder Aug 21 '17 at 12:52
  • \$\begingroup\$ 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! \$\endgroup\$ – AnonymousReality Aug 21 '17 at 14:52
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

WendyScript, 10 bytes

#:(x)[x]*x

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

V, 5 bytes

Du@"Ä

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 5 bytes

Super simple

.[QQY

Explanation:

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

R, 7 bytes

0*1:n+n

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – JAD Aug 23 '17 at 6:28
0
\$\begingroup\$

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

TI-BASIC, 12 bytes

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

Assumes an empty list.

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

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

\$\endgroup\$
0
\$\begingroup\$

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

Microscript II, 14 bytes

$s{o`s`+s}sNv*
\$\endgroup\$
0
\$\begingroup\$

C++, 86 56 45 bytes

Thanks to @ceilingcat for saving 30 41 bytes!

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Ruby, 12 bytes

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

13 bytes

Array.new n,n
\$\endgroup\$
0
\$\begingroup\$

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

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – ბიმო Jan 30 '18 at 20:08
  • \$\begingroup\$ @BMO: converting to a list is trivial: echo -n '['; echo 5 | dc dcscript | paste -sd, | tr -d \\n ; echo ']' \$\endgroup\$ – Thor Jan 30 '18 at 21:19
  • \$\begingroup\$ That would make it a bash submission though. \$\endgroup\$ – ბიმო Jan 30 '18 at 22:53
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Imo, if your language has no lists, use the next closest thing. \$\endgroup\$ – JAD Aug 23 '17 at 6:35
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

///, 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.
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Thanks, although I am still trying to figure out how I could get rid of the variables I needed to set \$\endgroup\$ – AleXYX Jan 31 '18 at 18:18
  • \$\begingroup\$ You should post that as your answer, not going to copy it. \$\endgroup\$ – AleXYX Jan 31 '18 at 18:26
  • \$\begingroup\$ That's up to you :) However I already have my own answer. \$\endgroup\$ – Mr. Xcoder Jan 31 '18 at 18:28
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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