16
\$\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\$
  • 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 '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
  • 7
    \$\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

103 Answers 103

1 2 3
4
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
| improve this answer | |
\$\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.

| improve this answer | |
\$\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.
| improve this answer | |
\$\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)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks, although I am still trying to figure out how I could get rid of the variables I needed to set \$\endgroup\$ – Mercury Platinum Jan 31 '18 at 18:18
  • \$\begingroup\$ You should post that as your answer, not going to copy it. \$\endgroup\$ – Mercury Platinum 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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

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
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ y* does it without using the codepoint exemption. y is the first line of standard input by default. \$\endgroup\$ – recursive Mar 26 '18 at 23:06
  • \$\begingroup\$ True, but "The elements need to be integers, not strings". \$\endgroup\$ – Weijun Zhou Mar 27 '18 at 1:21
0
\$\begingroup\$

12-basic, 17 14 bytes

?[X=INPUT()]*X

Try

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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)])
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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!

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

Kotlin, 20 19 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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 15 \$\endgroup\$ – ASCII-only Feb 22 '19 at 11:19
0
\$\begingroup\$

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)

| improve this answer | |
\$\endgroup\$
1 2 3
4

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.