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
    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
    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
    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\$ 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
    Aug 23, 2017 at 6:34

105 Answers 105

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\$
11
  • \$\begingroup\$ Ah, × vectorises these days, so to get list repetition, you need to embed it in a list? \$\endgroup\$
    – Neil
    Aug 21, 2017 at 12:14
  • \$\begingroup\$ Yeah, not sure if it's the best idea :/ but someone said so \$\endgroup\$
    – ASCII-only
    Aug 21, 2017 at 12:15
  • \$\begingroup\$ Never mind, NθEθθ is shorter even with non-vectorising ×. \$\endgroup\$
    – Neil
    Aug 21, 2017 at 12:16
  • \$\begingroup\$ Damnit lol I had this in mind, do you want to post a separate answer? \$\endgroup\$
    – ASCII-only
    Aug 21, 2017 at 12:18
  • \$\begingroup\$ Ah, Mold still works, that's good to know! \$\endgroup\$
    – Neil
    Aug 21, 2017 at 12:32
1
\$\begingroup\$

Integral, 6 Bytes

Outputs the list as individual stack elements joined by the newline.

If built-ins weren't banned, it would be possible via (1 bytes).

◙v◙♦▼e

Try it!

Explanation

◙      Duplicate input, in order to get around the edge case 1
 v◙    "◙": Create a copy of the operand, when evaluated
   ♦   Swap up a copy of the input
    ▼  Decrement
     e Repeat input-1 times: Duplicate stack
\$\endgroup\$
1
\$\begingroup\$

MAWP, 13 bytes

%@!1A[1A~!~]%

-15 bytes after integer input rework (Dion).

Try it!

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

Keg, 5 bytes

:(|:.

Try it online!

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

Arn, 7 bytes

|ÅF└¾c)

Explanation

Unpacked: v{_}\1=>

    \ Fold/map (in this case just map) with...
v{ A block with the key of v
  _ Input
}
      1=> Range, [1, in]
\$\endgroup\$
1
\$\begingroup\$

Braingolf, 6 bytes

!A.$_=

Try it online!

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.

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

[Swift 3], 55 49 bytes

func a(n:Int)->[Int] {return (0..<n).map{_ in n}}
\$\endgroup\$
2
  • \$\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, 2017 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\$ Aug 21, 2017 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
  • 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, 2017 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\$
2
  • \$\begingroup\$ No, Ans, , & dim(, 1 byte, L₁, 2 bytes, :, Ans, & (, 1 byte, L₁, 2 bytes, + & 1, 1 byte. \$\endgroup\$ Aug 23, 2017 at 21:27
  • \$\begingroup\$ Whoops, thought Ans( was all one byte, derp. \$\endgroup\$
    – Adalynn
    Aug 23, 2017 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
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\$
3
  • 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, 2018 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, 2018 at 21:19
  • \$\begingroup\$ That would make it a bash submission though. \$\endgroup\$ Jan 30, 2018 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\$

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