Turn an integer n into a list containing it n times

15

0

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.

Bruno E

Posted 2017-08-21T10:34:24.703

Reputation: 405

Why no builtins? – Okx – 2017-08-21T10:36:21.350

@Okx I meant built-in functions which already do this, must have used the wrong word, i'll edit the post. – Bruno E – 2017-08-21T10:37:42.030

I edited the question such that it is a bit more readable and fits our standard format better. – Mr. Xcoder – 2017-08-21T10:38:55.413

1Should n=0 be supported? – Arnauld – 2017-08-21T10:46:53.820

43

@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 – 2017-08-21T10:52:21.863

2Related. – Adám – 2017-08-21T11:06:05.183

With your current prohibition on built-ins, is this valid?

– Adám – 2017-08-21T11:18:29.290

1Does it matter? This is meant to be fun, we can have different opinions on what is and isn't valid. – Bruno E – 2017-08-21T11:51:03.527

13@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 – 2017-08-21T11:57:22.933

5@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 – 2017-08-21T12:42:49.877

in which case, I'll (begrudgingly) say the entry is invalid. – Bruno E – 2017-08-21T12:50:53.027

@Lynn then let the challenge be considered 'uninsteresting'. Nobody is under any obligation to answer, but a fair number of people seem to have found it interesting enough. – Bruno E – 2017-08-21T12:53:55.607

1Along with @Arnauld's comment, can we assume the input is non-negative? – kamoroso94 – 2017-08-21T23:21:35.560

6I'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 – 2017-08-22T07:14:59.993

8

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 – 2017-08-23T06:34:03.193

Can we leave the output on the stack for stack based languages, or do we have to print it? – MildlyMilquetoast – 2017-09-08T21:31:21.013

Answers

32

Jelly, 1 byte

x

Try it online!

Note that this is not the “repeat n n times” built-in — its function is more general than that. For example 4,5,6x1,2,3 equals [4, 5, 5, 6, 6, 6]. Given only one argument, Jelly just happens to use it as both the left and right argument for the supplied link, but this functionality is not inherent to x.

If this doesn’t count, there are various fun 2-byte alternatives:

x` ṁ` Ra Rị R» a€ oR oḶ oṬ oẊ Ḷị Ḷ» Ṭị Ṭ» Ẋị Ẋ» ị€ ṛ€ ȧ€ »€

etc.

Lynn

Posted 2017-08-21T10:34:24.703

Reputation: 55 648

No built-in functions that accomplish the task are allowed. While x may be more general, it is indeed a built-in function that accomplishes the task. – Adám – 2017-08-21T10:47:29.343

10Well, yes. Every code golf answer is a collection of built-ins that accomplish the task. You might ban this answer if x did “all the work”, but it most certainly doesn’t — there’s an implicit “0 bytes” of link-parsing and array-coercion logic going on to turn this into repeat([n], n), which is exactly what other answers do. – Lynn – 2017-08-21T10:48:45.777

1@Mr.Xcoder Who cares about intention? x is a built-in function. x accomplishes the task. No built-in function that accomplish the task are allowed. Therefore, x is not allowed. – Adám – 2017-08-21T10:51:23.430

@Lynn The fact that that is Jelly's normal way of treating missing arguments is exactly what makes x prohibited. APL's / (the grandfather of Jelly's x) and J's # (the father of Jelly's x) are not prohibited because they need and ~ respectively to explicitly use the input as both count and data. – Adám – 2017-08-21T10:59:09.223

3@Adám I think the difference lies in that this is not the function of the x command. When used in different cases, it provides different effects; it's no different than a J or APL answer with an implicit ~ or similar at the end of the program. – Conor O'Brien – 2017-08-21T11:05:56.080

@ConorO'Brien But it is. A complete documentation for x would have read when applied to one or more numbers, it returns a list where each element Xₙ is expanded to Xₙ copies of itself. – Adám – 2017-08-21T11:10:02.477

8@Adám This is the "repeat each element of x y times" function, taking 2 arguments. What makes it accomplish the task is how Jelly parses implicit arguments, which has nothing to do with the function itself. – Erik the Outgolfer – 2017-08-21T12:07:10.163

6If this answer is invalid then the constraint of the question is not "No builtins" it is "No 1 byte answers", which is an extremely arbitrary limitation that seems counterproductive in code golf. – Kamil Drakari – 2017-08-21T13:44:20.113

8I think an argument towards this not being a built-in is that there are two other 1 byte answers in Jelly that do the same thing and so this answer could be any one of the three. There aren't 3 built-ins (one would hope) for "repeat n n times", so they can't ALL be a "built-in" for it. – nmjcman101 – 2017-08-21T14:03:38.023

2

@nmjcman101 I disagree. In my APL answer you see three built-ins that almost do the task; they just need to be told to use a single argument twice. This "telling" is built into Jelly. Whether you call it part of its primitives or part of the language is just a matter of semantics. All three are Jelly built-ins for the exact job. Compare to multiplication and minimum on Booleans. Wouldn't both be prohibited in a challenge to AND two Booleans without built-ins for the exact task?

– Adám – 2017-08-21T18:05:18.510

@Adám I would say that multiplication and minimum are not builtins for boolean AND, even if they complete a challenge which requested boolean AND. Perhaps this should be made a meta-post for further discussion? – Kamil Drakari – 2017-08-21T21:15:24.430

@KamilDrakari K has a single built-in & labelled min|and.

– Adám – 2017-08-21T21:19:26.940

@Adám I've posted this meta post for further discussion on this topic.

– Kamil Drakari – 2017-08-21T21:38:59.670

6This whole comment thread seems a good argument for not banning something as subjective as builtins. – trichoplax – 2017-08-26T20:35:23.030

22

Python 3, 14 bytes

lambda k:[k]*k

Try it online!

Mr. Xcoder

Posted 2017-08-21T10:34:24.703

Reputation: 39 774

What's wrong with just [n]*n? – None – 2017-08-23T03:19:55.520

3@paxdiablo That is a snippet. We can create either full programs or functions as per the meta consensus. – Mr. Xcoder – 2017-08-23T06:50:13.220

19

Operation Flashpoint scripting language,  50  46 bytes

f={a=[];t=_this;while{count a<t}do{a=a+[t]};a}

Call with:

hint format["%1", 5 call f]

Output:

Steadybox

Posted 2017-08-21T10:34:24.703

Reputation: 15 798

Do you have post decrement, i--, and += in this? – TheLethalCoder – 2017-08-21T13:51:09.950

@TheLethalCoder Unfortunately, no. – Steadybox – 2017-08-21T14:16:03.300

3Why are the screenshots for this always in a barren desert? You should use a cool map for your screenshots :P. – Magic Octopus Urn – 2017-08-21T15:42:59.273

2@MagicOctopusUrn Is that better? – Steadybox – 2017-08-21T15:53:46.003

1@Steadybox ahaha! Epic :P. Is that the town from left for dead? That looks like the church you start in kinda. – Magic Octopus Urn – 2017-08-21T16:07:19.037

@MagicOctopusUrn No, it's just a town from one of the game's original maps. – Steadybox – 2017-08-21T16:15:02.320

I always upvote OPFP – FantaC – 2018-01-31T04:37:07.623

1I think the screenshots should be posted cropped to a smaller image. We don't need to see the map, and it distracts from the actual output. – mbomb007 – 2018-05-10T20:13:14.503

12

APL (Dyalog), 2 bytes

Five equally short solutions. Last two are courtesy of Zacharý.


⍴⍨

Try it online!

 cyclically reshape

 self


/⍨

Try it online!

/ replicate

 self


\⍨

Try it online!

\ expand

 self


⌿⍨

Try it online!

 replicate along first (and only) axis

 self


⍀⍨

 expand along first (and only) axis

 self

Try it online!

Adám

Posted 2017-08-21T10:34:24.703

Reputation: 37 779

@Uriel There's one more… – Adám – 2017-08-21T11:12:13.683

1Both ⌿⍨ and ⍀⍨ work. – Zacharý – 2017-08-21T14:11:13.043

2Was it intentional that 'self' looks like a face? – geokavel – 2017-08-21T16:12:38.627

1@geokavel I don't think so, but I also noticed it as a typed this post. Makes it pretty mnemonic, no? In fact, selfie is one of its semi-official names, so in the RIDE interface to Dyalog APL, you can insert by typing ``selfie. – Adám – 2017-08-21T18:18:18.360

⍀ and ⍨ are each three-byte characters. TIO does report the bytesize correctly. I feel like you should know this. – canhascodez – 2017-08-22T19:16:17.267

1

@sethrin TIO counts characters (and UTF-8 bytes where applicable), but it is up to the user to ensure that they do not use any characters that are missing from a single-byte character set (SBCS). For APLs, see here.

– Adám – 2017-08-22T19:20:40.017

@Adám thanks for the correction. I'll have to read more about APL's charset. – canhascodez – 2017-08-22T19:29:09.933

11

Mathematica, 10 bytes

#~Table~#&

J42161217

Posted 2017-08-21T10:34:24.703

Reputation: 15 931

10

Proton, 8 bytes

n=>[n]*n

Try it online!

totallyhuman

Posted 2017-08-21T10:34:24.703

Reputation: 15 378

11This combines the best of the python answer and the javascript answer, I love it! – Bruno E – 2017-08-21T10:48:42.337

9

JavaScript (ES6), 19 bytes

n=>Array(n).fill(n)

Try it

o.innerText=(f=
n=>Array(n).fill(n)
)(i.value=8);oninput=_=>o.innerText=f(+i.value)
<input id=i type=number><pre id=o>

Shaggy

Posted 2017-08-21T10:34:24.703

Reputation: 24 623

3An array without ... - whatever next?! – Neil – 2017-08-21T12:11:49.223

1@Neil: it felt wrong even as I was typing it! :D – Shaggy – 2017-08-21T17:05:40.923

9

Octave, 12 bytes

@(n)~(1:n)+n

Try it online!

rahnema1

Posted 2017-08-21T10:34:24.703

Reputation: 5 435

What does the ~ in octave, because that's the only part of your code I don't understand.. – Michthan – 2017-08-24T10:57:14.753

1@Michthan Sorry for late reply. ~ is the not operator that converts 1:n to an array of 0s of size n. You can use ! instead of it. – rahnema1 – 2017-08-24T13:42:45.533

7

Pyth, 2 bytes

*]

Test suite.


*]QQ - Full program with implicit input

 ]   - Turn the input into a list.
*    - Repeat it a number of times equal to the input. 

Mr. Xcoder

Posted 2017-08-21T10:34:24.703

Reputation: 39 774

7

Haskell, 13 bytes

f n=n<$[1..n]

Try it online! Usage: f 5 yields [5,5,5,5,5]. For n=5, [1..n] yields the list [1,2,3,4,5]. n<$ replaces each element of this list with n.

Laikoni

Posted 2017-08-21T10:34:24.703

Reputation: 23 676

3Mine's a bit longer, but I like it anyway: join replicate – amalloy – 2017-08-23T00:13:47.243

@amalloy This would indeed be the clean Haskell way to do it. However, join is not part of Prelude and thus requiers a lengthy import Control.Monad, which rarely makes it useful for golfing. – Laikoni – 2017-08-23T08:42:32.800

6

R, 18 bytes

array(n<-scan(),n)

Try it online!

user2390246

Posted 2017-08-21T10:34:24.703

Reputation: 1 391

6Is rep(n<-scan(),n) too close to a builtin? – Robin Gertenbach – 2017-08-21T16:37:20.240

@RobinGertenbach Maybe you should post it as a separate answer. – Andreï Kostyrka – 2018-03-24T22:05:29.090

5

05AB1E, 2 bytes

Try it online!

Emigna

Posted 2017-08-21T10:34:24.703

Reputation: 50 798

Nice, better than .D). – Magic Octopus Urn – 2017-08-21T15:40:50.273

@MagicOctopusUrn: Yeah, that was my first thought. – Emigna – 2017-08-21T15:53:29.277

5

Dodos, 76 bytes

	f f r 2
2
	
	
r
	r d
	f s t f
d
	dip f s t
	f
t
	dot f
	dot
s
	s dip
f
	dab

Try it online!

Explanation:

f is an alias for dab (tail).

s is subtraction, as explained on the wiki: (x, y) → (0, y−x) when x ≤ y.

t maps (a, b, c…) to (b+c+…, a+b+c+…).

f s t maps (a, b, c…) to a. This is our “head” function.

d dips only the head of its argument: (a, b, c…) → (|a−1|, b, c…)

r is the main repetition logic. We map (a, b) to (*r(|a−1|, b), b).

For example, r(4, 7) will evaluate as

  r(4, 7)
= r(3, 7), 7
= r(2, 7), 7, 7
= r(1, 7), 7, 7, 7
= r(0, 7), 7, 7, 7, 7
  → This would call r(1, 7), but (1, 7) ≥ (0, 7), so surrender!
= 0, 7, 7, 7, 7, 7.

Finally, we define 2, which maps n → (n, n), and define main as f f r 2, computing r(n, n) and chopping off the first two elements.

Lynn

Posted 2017-08-21T10:34:24.703

Reputation: 55 648

4

Japt, 2 bytes

ÆU

Test it


Explanation

Implicit input of integer U. Generate an array of integers from 0 to U-1. Fill it with U. Implicit output of resulting array.

Shaggy

Posted 2017-08-21T10:34:24.703

Reputation: 24 623

4

Perl 5, 18 14 bytes

-4 bytes thanks to @DomHastings

sub{(@_)x"@_"}

Try it online!

Is x a builtin that does the entire task? Sort of? Not really? Rules unclear?

Edit: Yeah, probably it's fine.

aschepler

Posted 2017-08-21T10:34:24.703

Reputation: 717

Had pretty much the same, but you can change the first $_[0] to @_! Also the second can be"@_" I think... – Dom Hastings – 2017-08-21T13:10:12.313

I would say it doesn't count as a built-in because you have to work around the fact that it takes two inputs instead of one. – Brad Gilbert b2gills – 2017-08-21T16:07:15.353

Why not $_=$_ x$_ with perl -pe? – Thor – 2017-08-22T06:45:56.357

@Thor x does string repetition, not list repetition, unless the left operand is in parentheses (or is a qw operator) and the x is evaluated in list context. And of course $_ is a scalar, not a list. – aschepler – 2017-08-22T23:24:09.437

I meant something like this: echo 5 | perl -pe '$_=$_ x$_' – Thor – 2017-08-22T23:49:46.647

1@Thor I wouldn't count that as satisfying "return a list". – aschepler – 2017-08-23T22:54:31.437

4

Java (OpenJDK 8), 50 48 bytes

n->java.util.Arrays.stream(new int[n]).map(i->n)

Try it online!

-2 bytes thanks to @Jakob

Inspired by the comments in @OlivierGrégoire's post, and optimized a little further. Takes an integer input, creates an IntStream of n elements, then maps each element to n and returns it.

Xanderhall

Posted 2017-08-21T10:34:24.703

Reputation: 1 236

You can save 2 bytes by starting with java.util.Arrays.stream(new int[n]). – Jakob – 2017-08-21T19:23:50.800

4

Haskell (14 bytes)

replicate>>=id

Thanks to @nimi, I don't need any import anymore. Yay!

It's a function that takes an integer argument; for example, the following returns [5,5,5,5,5]:

(replicate>>=id) 5

tomsmeding

Posted 2017-08-21T10:34:24.703

Reputation: 2 034

1Why not id=<<replicate? It's also 14 bytes but doesn't need the import. – nimi – 2017-08-21T22:44:50.940

@nimi Very good point! Overlooked that possibility. (I really need to dive into the arrow monad more...) – tomsmeding – 2017-08-22T07:31:39.813

4

TeX, 81 bytes

\newcommand{\f}[1]{#1\count0=2\loop,#1\advance\count0 by1\ifnum\count0<#1\repeat}

Usage

\documentclass[12pt,a4paper]{article}
\begin{document}
\newcommand{\f}[1]{#1\count0=2\loop,#1\advance\count0 by1\ifnum\count0<#1\repeat}

\f{5}

\f{10}
\end{document}

enter image description here

Leaky Nun

Posted 2017-08-21T10:34:24.703

Reputation: 45 011

That's actually LaTeX. In Tex it'd be much shorter. – A Gold Man – 2017-09-08T05:22:00.670

4

Husk, 2 bytes

´R

Try it online!

´    -- Apply next function twice to same argument
 R   -- given an integer n and some element, replicate the element n-times

Polite alternative (3 bytes)

ṠIR

Try it online!

Laikoni

Posted 2017-08-21T10:34:24.703

Reputation: 23 676

3

J, 2 bytes

$~

Same as the APL answer: reflexively shape the input. In other words:

$~ y
y $ y
NB. y copies of y

Conor O'Brien

Posted 2017-08-21T10:34:24.703

Reputation: 36 228

3

Brainbash, 39 bytes

>-[-[-<]>>+<]>->#[->+>+<<]>>[-<;<<.>>>]

Try it online!

Prints N N times. Works by generating 32, taking input, then duplicating the input twice, then output the first for each 1 in the second.

Conor O'Brien

Posted 2017-08-21T10:34:24.703

Reputation: 36 228

3

C (gcc), 55 bytes

int*f(k){int*r=malloc(k*4),a=k;for(;a-->0;)r[a]=k;k=r;}

Try it online!

Returns a list of k integers.

Conor O'Brien

Posted 2017-08-21T10:34:24.703

Reputation: 36 228

1Yay "long arrow operator". Also, I didn't think gcc would ever use register eax for locals. Go figure. – aschepler – 2017-08-21T12:22:51.410

2You can save 2 bytes by removing a comparison to 0 in for cycle, unless I've overlooked something. – Jasmes – 2017-08-21T15:45:13.733

Suggest *f(k){int r[k], instead of int*f(k){int*r=malloc(k*4), – ceilingcat – 2017-08-22T20:39:15.140

3

Röda, 10 bytes

{[[_]*_1]}

Try it online!

Explanation:

{[[_]*_1]}
{        } /* Anonymous function   */
   _       /* The input (_1)       */
  [ ]      /* As a list            */
     *_1   /* Repeated _1 times    */
 [      ]  /* Pushed to the stream */

fergusq

Posted 2017-08-21T10:34:24.703

Reputation: 4 867

1Why can you leave off the 1 in the first input but not the second? – Conor O'Brien – 2017-08-21T22:20:49.237

1@ConorO'Brien Each underscore without a number has a number that is one larger than the previous: [_]*_ = [_1]*_2. Because the first underscore is the first, it has automatically the number 1. – fergusq – 2017-08-22T05:29:30.170

3

Coreutils, sed, 14 bytes

yes $1|sed $1q

As a zsh function, 20 19 bytes:

f(){yes $1|sed $1q}

Try it online!

Thor

Posted 2017-08-21T10:34:24.703

Reputation: 2 526

I don't think this answer is valid, since it doesn't take input. – James – 2017-08-21T13:14:52.497

@DJMcMayhem: used it in a function – Thor – 2017-08-21T14:08:33.853

Why not just make it a program yes $1|sed $1q? – Digital Trauma – 2017-08-21T20:54:13.543

Good point @DigitalTrauma, updated – Thor – 2017-08-22T06:39:41.070

The rules say it has to be integer elements, not string elements. To satisfy that, a bash/zsh answer would need to use declare -i integer variables. But it also has to be an array. I'm not sure bash even supports an integer array (like eval declare -ia "$1" to use the first function arg as the name of an array return value.) I upvoted this because it follows the spirit of the question; I doubt the question meant to exclude languages that don't really have integer lists / arrays. – Peter Cordes – 2017-08-23T21:05:30.343

3

MATL, 4 3 bytes

tY"

Try it online!

Explanation:

t       % duplicate elements
Y"      % replicate elements of array
        % (implicit) convert to string and display

Cinaski

Posted 2017-08-21T10:34:24.703

Reputation: 1 588

3

Groovy, 9 bytes

{[it]*it}

Try it online!


Perhaps the most competitive groovy answer I've done to date.

Magic Octopus Urn

Posted 2017-08-21T10:34:24.703

Reputation: 19 422

3

brainfuck, 16 bytes

[->+>+<<]>[->.<]

Try it online!

The breakdown:

[->+>+<<]          Duplicate 'n' into the next 2 cells to the right
         >         Move to the first duplicate
          [->.<]   Print 'n', 'n' times

As I'm sure you're aware, brainfuck takes input and output values as ASCII characters. So a ! is represented as the value 33.

Graviton

Posted 2017-08-21T10:34:24.703

Reputation: 2 295

Your program doesn't take input, I don't think. Unless you're talking about a value left on the tape – Conor O'Brien – 2017-08-21T22:26:39.423

@ConorO'Brien The ladder. Think of it as a function more than a program. – Graviton – 2017-08-21T22:27:33.420

2

Java (OpenJDK 8), 58 56 bytes

n->{int a[]=new int[n],i=n;for(;i-->0;)a[i]=n;return a;}

Try it online!

-2 bytes thanks to @KevinCruijssen

Olivier Grégoire

Posted 2017-08-21T10:34:24.703

Reputation: 10 647

1Two bytes shorter: n->{int a[]=new int[n],i=n;for(;i-->0;)a[i]=n;return a;} – Kevin Cruijssen – 2017-08-21T11:15:46.677

@KevinCruijssen Ouch, it hurts I didn't think about that... Thanks! – Olivier Grégoire – 2017-08-21T11:58:42.423

Happens to the best of us. ;) If you look in my answer history you'll probably also find some answers where I add something along the lines of "bytes saved thanks to ... due to a stupid mistake by myself / something obvious I forgot.." :) – Kevin Cruijssen – 2017-08-21T12:01:12.353

I thought about an answer like IntStream.generate(() -> n).limit(n) but decided it wasn't worth typing up and upvoted this instead :) – JollyJoker – 2017-08-21T12:42:57.620

1

@JollyJoker You could do it! It's indeed two bytes shorter and would easily beat my answer ;-)

– Olivier Grégoire – 2017-08-21T12:57:33.293

Yeah, but it's also flat out the canonical correct way of doing that in Java 8 so it's a pretty boring answer – JollyJoker – 2017-08-21T13:03:19.827

Isn't using Arrays.fill(a,n)wouldn't be shorter than looping? (I'm new to PPCG, don't know if java.util can be used). – Jean-Baptiste Yunès – 2017-08-23T17:22:23.660

@Jean-BaptisteYunès If you want to try that, compare the two following: i=n;for(;i-->0;)a[i]=n; and java.util.Arrays.fill(a); (yes, the full name is required). 23 bytes vs. 25 bytes. I think I'll keep the loop. But thanks anyways ;-) – Olivier Grégoire – 2017-08-23T17:51:15.940

Ok I was not aware about full name qualification. I tried to find the rules but wasn't able? Any hint please? – Jean-Baptiste Yunès – 2017-08-24T06:40:08.110

2

Swift 3, 29 bytes

{n in(0..<n).map{_ in n}}

Try it here!

Swift 3, 30 bytes

{Array(repeating:$0,count:$0)}

Try it here!

Mr. Xcoder

Posted 2017-08-21T10:34:24.703

Reputation: 39 774

I wonder if we can get the input as the same variable repeated twice. Then you could have a 10 byte solution: Array.init. Just assign it to a variable with a type alias, and boom: let f: (Int, Int) -> [Int] = Array.init; print(f(5, 5)) – Alexander - Reinstate Monica – 2017-09-21T06:41:15.953

@Alexander No, we cannot get the input twice. – Mr. Xcoder – 2017-09-21T06:52:47.297

2

C# (Mono), 38 37 bytes

n=>System.Linq.Enumerable.Repeat(n,n)

Old version for 38 bytes:

n=>"".PadLeft(n,(char)n).ToCharArray()

Try it online!

A char in C# is an int underneath so this appears to be valid.


C# (Mono), 44 bytes

using System.Linq;n=>new int[n].Select(_=>n)

If the above is invalid this is for an extra 6 bytes.

Try it online!

TheLethalCoder

Posted 2017-08-21T10:34:24.703

Reputation: 6 930

Also 38 Bytes but more readable: n=>new String((char)n,n).ToCharArray() – TheJoeIaut – 2017-08-21T16:20:30.933

@TheJoeIaut So you're aware that would have to be new string... note the lower case :) Though yes it is the same length unfortunately. – TheLethalCoder – 2017-08-21T16:22:01.423

25: n=>Enumerable.Repeat(n,n) – Manfred Radlwimmer – 2017-08-24T10:08:46.233

@ManfredRadlwimmer Actually 37 but does save a byte thanks! – TheLethalCoder – 2017-08-24T10:10:02.290

Oh, right - the using – Manfred Radlwimmer – 2017-08-24T10:10:36.983

@ManfredRadlwimmer Well you can fully qualify it here :) – TheLethalCoder – 2017-08-24T10:10:51.890

2

Tampio, 109 85 bytes

a:n uni on a a:na
nolla a:na on tyhjyys
a:n seuraaja o:na on o lisättynä a:han o:na

Explanation:

a:n uni on   a a:na
uni(a)  =  f(a,a)

  nolla a:na on tyhjyys
f(0,    a)   =  []

  a:n seuraaja o:na on o lisättynä a:han o:na
f(a   +1,      o)   =  o :       f(a,    o)

uni(a)   = f(a,a)
f(0  ,a) = []
f(a+1,o) = o : f(a,o)

fergusq

Posted 2017-08-21T10:34:24.703

Reputation: 4 867

2

PHP, 19 Bytes

I'm new here can anyone tell if this is ok ?

for(;$b--;)$a[]=$n;

try it online!

EDIT:

looks to not be a valid form so :

32 Bytes (by Titus)

(+1 Byte for R flag)

for($b=$argn;$b--;)echo$argn,_;

try it online!

Frankich

Posted 2017-08-21T10:34:24.703

Reputation: 121

1

Use Try It Online: https://tio.run/#php

– Nnnes – 2017-08-21T12:18:42.270

@Nnnes it's better like this ? – Frankich – 2017-08-21T12:28:30.567

2Wellcome to PPGC. You need to use some form of input and output you're not allowed to handle that outside. Use e.g. $_GET, $argv, $argn or read from stdin. for($a=$argn;$argn--;)$b[]=$a;echo implode(',',$b); would work. echo$a=$argn;for(;--$argn;)echo',',$a; would be better though ;) You would be allowed to submit a function but they are usally longer in php :/ – Christoph – 2017-08-22T06:24:33.333

function a($b){for($n=$b;$b--;)$a[]=$n;return$a;} would be valid for example. – Christoph – 2017-08-22T06:29:29.577

... or for($b=$argn;$b--;)echo$argn,_; (and don´t forget to count +1 for the R flag) – Titus – 2017-08-22T06:34:29.580

@Titus i don't get it about the $argn and the flag, i understand that you do it by command line but i don't find a way to use input for command on tio.run (feel free to edit if it's necessary) – Frankich – 2017-08-22T06:58:22.657

afaik, TiO cannot handle $argn. Just define a variable with that name in the header. – Titus – 2017-08-22T07:11:38.460

2

cQuents v0, 3 bytes

::n

Try it online!

Explanation

::    Mode: sequence 2. Given input n, output the sequence n times
  n   Each item in the sequence is n

Stephen

Posted 2017-08-21T10:34:24.703

Reputation: 12 293

Note current version uses & instead of :: – Stephen – 2019-02-01T04:51:20.513

2

Common Lisp, 31 bytes

(lambda(x)(fill(make-list x)x))

Try it online!

Renzo

Posted 2017-08-21T10:34:24.703

Reputation: 2 260

2

Perl 6, 9 bytes

{$_ xx$_}

Try it online

Uses the built-in list/statement repeat operator xx

Brad Gilbert b2gills

Posted 2017-08-21T10:34:24.703

Reputation: 12 713

2

Kotlin, 22 bytes

{n:Int->(1..n).map{n}}

Try it online!

tkw

Posted 2017-08-21T10:34:24.703

Reputation: 21

1Welcome to the site! :) – James – 2017-08-21T16:40:54.633

2

Element, 9 bytes

_2:'[2:`]

Try it online!

_        input 
2:       double the input 
'        push one to control stack
[2:`]    for (number), double it, output one of them

aAaa aAaa

Posted 2017-08-21T10:34:24.703

Reputation: 141

2

Ruby, 10 characters

->n{[n]*n}

Sample run:

irb(main):001:0> ->n{[n]*n}[5]
=> [5, 5, 5, 5, 5]

Try it online!

Ruby, 18 characters

->n{Array.new n,n}

Sample run:

irb(main):001:0> ->n{Array.new n,n}[5]
=> [5, 5, 5, 5, 5]

Try it online!

manatwork

Posted 2017-08-21T10:34:24.703

Reputation: 17 865

2

PowerShell, 13 10 bytes

{@($_)*$_}

Usage

PS C:\> 2 | % {@($_)*$_}
2
2

And:

PS C:\> 5 | % {@($_)*$_}
5
5
5
5
5

By default, PowerShell shows array content one element per line. Running it through the ConvertTo-Json cmdlet shows it in a more familiar form:

PS C:\> 5 | % {@($_)*$_} | ConvertTo-Json -Compress
[5,5,5,5,5]

Don Cruickshank

Posted 2017-08-21T10:34:24.703

Reputation: 131

Can you call this with 5 | % {@($_)*$_}? If so you don't need the 3 bytes ($f=) required to name the function. – Post Rock Garf Hunter – 2017-08-22T19:21:34.107

Yes it can. Thanks! – Don Cruickshank – 2017-08-22T19:32:03.303

2

TI Basic, 10 bytes

This assumes an empty list

Ans→dim(L₁:L₁+Ans

Here's the hex, along with the code to explain the byte count:

72  04 B5   5D 00 3E 5D 00 70 71
Ans →  dim( L₁    :  L₁    +  Ans

How?

Ans→dim(L₁:L₁+Ans
Ans→dim(L₁        Since the list is empty, this makes L₁ a list on Ans 0's
          :       Separator
           L₁+Ans Since TI-Basic vectorizes some commands,
                  this adds Ans (basically the input here) to each element in L₁,
                  which results in Ans for each element.

Zacharý

Posted 2017-08-21T10:34:24.703

Reputation: 5 710

Ans+0rand(Ans for 5 bytes – Oki – 2017-08-25T21:38:13.587

Thanks, I'll post that in another solution, since it's pretty different! – Zacharý – 2017-08-25T22:51:27.233

Actually, that's six bytes, rand( isn't a thing. It's Ans + 0 rand ( Ans. – Zacharý – 2017-08-25T23:05:43.763

You're right. Instincts.. – Oki – 2017-08-25T23:11:30.537

Also should mention that on newer calculators (CSE?) you can use ceiling() to save a byte – Oki – 2017-08-25T23:20:53.520

2

TI Basic, 6 bytes

Ans+0rand(Ans

Thanks to @Oki for this solution!

Zacharý

Posted 2017-08-21T10:34:24.703

Reputation: 5 710

0 can be replaced with lots of things (int(/iPart(, imag(, angle(, ...). Alternative solution: Ansnot(not(rand(Ans. Other noteworthy solutions (1 byte longer) randInt(Ans,Ans,Ans, Ansrand(Ans)^0, Ans(1^rand(Ans, -Ansint(-rand(Ans – Oki – 2017-08-26T11:14:03.907

1

Recursiva, 4 bytes

*aAa

Try it online!

Explanation:

*a  : Multiply a times(Here, 8) [8,8,8,8,8,8,8,8] 
  A : List-ify a [8]
   a: All a's will be assigned with the first input argument) 8 

officialaimm

Posted 2017-08-21T10:34:24.703

Reputation: 2 739

1

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

ASCII-only

Posted 2017-08-21T10:34:24.703

Reputation: 4 687

Ah, × vectorises these days, so to get list repetition, you need to embed it in a list? – Neil – 2017-08-21T12:14:25.143

Yeah, not sure if it's the best idea :/ but someone said so – ASCII-only – 2017-08-21T12:15:36.997

Never mind, NθEθθ is shorter even with non-vectorising ×. – Neil – 2017-08-21T12:16:27.843

Damnit lol I had this in mind, do you want to post a separate answer?

– ASCII-only – 2017-08-21T12:18:05.023

Ah, Mold still works, that's good to know! – Neil – 2017-08-21T12:32:35.790

Old McDonald had an integer list, EIθIθ... well, almost! Would EIθθ be a valid answer? – Neil – 2017-08-21T12:34:21.597

Unfortunately no :P the OP states that "The elements need to be integers, not strings." – ASCII-only – 2017-08-21T12:36:45.070

Save a byte by using . – Erik the Outgolfer – 2017-08-21T14:15:41.113

Oh yeah oops lol will do when I get back on a computer – ASCII-only – 2017-08-21T14:16:51.427

Surely this isn't 35 bytes. I don't know Charcoal, but I'd guess it's in a format such that it's 4, right? – cole – 2017-08-23T22:55:15.050

@cole Oops lol copied directly from TIO, thanks for pointing it out – ASCII-only – 2017-08-23T22:55:57.653

1

Ohm, 4 bytes

DÄWî

Try it online!

DÄWî

D     duplicate
 Ä    push n onto the stack n times
  W   wrap the stack in a list
   î  cast to integer

totallyhuman

Posted 2017-08-21T10:34:24.703

Reputation: 15 378

1

Pyke, 3 bytes

]1*

Try it here!

]1  - Wrap the input into a list.
  * - Repeat the list.

Mr. Xcoder

Posted 2017-08-21T10:34:24.703

Reputation: 39 774

1

J, 2 bytes

#~

Try it online!

# copy

~ self

Adám

Posted 2017-08-21T10:34:24.703

Reputation: 37 779

1

Pari/GP, 16 bytes

n->[n|x<-[1..n]]

Try it online!

Or:

n->vector(n,x,n)

Try it online!

Or:

n->powers(1,n,n)

Try it online!

alephalpha

Posted 2017-08-21T10:34:24.703

Reputation: 23 988

1

Stacked, 6 bytes

[:rep]

Try it online!

This is an anonymous function that takes input on the stack and leaves output on the stack. This simply takes the input, duplicates it, then repeats the input by the input.

Conor O'Brien

Posted 2017-08-21T10:34:24.703

Reputation: 36 228

1

QBasic 4.5, 48 bytes

INPUT a
DIM t(a)
FOR x=1TO a
t(x)=a
NEXT

steenbergh

Posted 2017-08-21T10:34:24.703

Reputation: 7 772

1

Aceto, 3 bytes

Assumes the integer is on the stack already.

This just duplicates the top stack element and then performs stack multiplication (i.e. Python's [x]*y).

L3viathan

Posted 2017-08-21T10:34:24.703

Reputation: 3 151

1

SOGL V0.12, 2 bytes

∙r

Try it Here!
Outputs to the stack.
Explanation:

∙   multiply POP (string, defaults to input) by POP (number, defaults to the same input)
     vertically - so to an array of strings
 r  swap types - convert the strings to numbers

dzaima

Posted 2017-08-21T10:34:24.703

Reputation: 19 048

1

Brain-Flak, 28, 22 bytes

{(({}[()])<>[]())<>}<>

Try it online!

Normally, the version that uses one stack would be shorter. However, in this case it's 6 bytes longer:

(({})){({}<(({}))>[()])}{}{}

James

Posted 2017-08-21T10:34:24.703

Reputation: 54 537

One stack in 24 bytes. – Riley – 2017-08-21T13:34:51.797

1

Gaia, 2 bytes

Try it online!

w   Wrap input into list
 ×  Repeat (input) times

Business Cat

Posted 2017-08-21T10:34:24.703

Reputation: 8 927

1

MY, 6 bytes

ωi×ω+↵

Try it online!

How?

  • ω, push the first command line argument evaluated
  • i, pop n; push [1...n]
  • ×, pop a; pop b; push b*a. 0 is popped when there is nothing on the stack. Since commands vectorize ("vecify" in MY), it ends up being an array of n 0's.
  • ω, push the first command line argument evaluated
  • +, pop a; pop b; push b+a. Adds the command line argument to every item in the array of 0 repeated n times, giving us n repeated n times
  • , pop n; print n with a newline.

Zacharý

Posted 2017-08-21T10:34:24.703

Reputation: 5 710

1

Prolog (SWI), 33 bytes

X*L:-maplist(=(X),L),length(L,X).

Try it online!

Emigna

Posted 2017-08-21T10:34:24.703

Reputation: 50 798

1

Tcl, 22 bytes

time {lappend L $n} $n

Try it online!


With built-ins:

Tcl, 13 bytes

lrepeat $n $n

Try it online!

sergiol

Posted 2017-08-21T10:34:24.703

Reputation: 3 055

1

MacOS bash, 12

jot $1 $1 $1

This also works on Linux if the jot package is installed, e.g. sudo apt install athena-jot.

Try it online.

Digital Trauma

Posted 2017-08-21T10:34:24.703

Reputation: 64 644

1

Actually, 2 bytes

Explanation:

;α
;   duplicate the input
 α  push a list containing n copies of n

α is not a builtin for this challenge by the usual definition, because it does not solve this challenge by itself. α takes the top stack element (call it a) and repeats the next stack element (b) a times. It's roughly equivalent to iterable multiplication in Python ([5, 1]*2 == [5, 5, 1, 1]), but also works on non-iterables (52α yields [5, 5]).

Try it online!

Mego

Posted 2017-08-21T10:34:24.703

Reputation: 32 998

1

4, 32 bytes

3.700001000260201801500101010294

Try it online!

If you have concerns regarding the input format, please read this first.

Transpiles roughly as

grid[1] = grid[0] = input()
while grid[1] != 0:
  print(grid[0])
  grid[1] -= 1

Uriel

Posted 2017-08-21T10:34:24.703

Reputation: 11 708

this can only handle single-digit numbers properly – Timothy Groote – 2017-08-23T11:23:09.447

@TimothyGroote no; read the meta post. this handles numbers as bytes, because that's the main form of input for that language. input 1 is 48, and will print 1 (byte representation) 48 times. – Uriel – 2017-08-23T13:55:06.950

1

Braingolf, 6 bytes

!A.$_=

Haven't had Dennis pull this version to TIO yet, but A used to be useless, it basically acted the same as & in most cases. Now it pops the top of the stack and repeats the next operator that many times.

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.

"Proof"

Yes I use zsh

Skidsdev

Posted 2017-08-21T10:34:24.703

Reputation: 9 656

1

Clojure, 13 bytes

#(repeat % %)

MattPutnam

Posted 2017-08-21T10:34:24.703

Reputation: 521

1

Coq, 68 bytes

fun n=>(fix f k n:=match n with|0=>nil|S n=>(k::f k n)%list end) n n

Of course there's a library function, giving 46 bytes:

Require List. Definition f n:=List.repeat n n.

Elazar

Posted 2017-08-21T10:34:24.703

Reputation: 131

1

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!

Firen

Posted 2017-08-21T10:34:24.703

Reputation: 11

1

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!

mudkip201

Posted 2017-08-21T10:34:24.703

Reputation: 833

1

FORTH 21 Bytes

: B 0 DO DUP . LOOP ;

OUTPUT:

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

panicmore

Posted 2017-08-21T10:34:24.703

Reputation: 51

1

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

Bubbler

Posted 2017-08-21T10:34:24.703

Reputation: 16 616

0

[Swift 3], 55 49 bytes

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

AnonymousReality

Posted 2017-08-21T10:34:24.703

Reputation: 41

$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 – 2017-08-21T12:52:57.290

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 – 2017-08-21T14:52:27.027

0

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!

Steadybox

Posted 2017-08-21T10:34:24.703

Reputation: 15 798

0

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.

idrougge

Posted 2017-08-21T10:34:24.703

Reputation: 641

0

WendyScript, 10 bytes

#:(x)[x]*x

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

Try it online!

Felix Guo

Posted 2017-08-21T10:34:24.703

Reputation: 211

0

V, 5 bytes

Du@"Ä

Try it online!

James

Posted 2017-08-21T10:34:24.703

Reputation: 54 537

0

Pyth, 5 bytes

Super simple

.[QQY

Explanation:

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

Try it online!

Stan Strum

Posted 2017-08-21T10:34:24.703

Reputation: 436

0

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.

Chaos Manor

Posted 2017-08-21T10:34:24.703

Reputation: 521

0

R, 7 bytes

0*1:n+n

Try it online!

Alex Yates

Posted 2017-08-21T10:34:24.703

Reputation: 17

1This 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 – 2017-08-23T06:28:11.603

0

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

Taylor Scott

Posted 2017-08-21T10:34:24.703

Reputation: 6 709

0

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

Taylor Scott

Posted 2017-08-21T10:34:24.703

Reputation: 6 709

Imo, if your language has no lists, use the next closest thing. – JAD – 2017-08-23T06:35:28.603

0

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)

Lyndon White

Posted 2017-08-21T10:34:24.703

Reputation: 1 021

0

TI-BASIC, 12 bytes

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

Assumes an empty list.

Scott Milner

Posted 2017-08-21T10:34:24.703

Reputation: 1 806

No, Ans, , & dim(, 1 byte, L₁, 2 bytes, :, Ans, & (, 1 byte, L₁, 2 bytes, + & 1, 1 byte. – Scott Milner – 2017-08-23T21:27:01.367

Whoops, thought Ans( was all one byte, derp. – Zacharý – 2017-08-23T21:33:33.887

0

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

Asleepace

Posted 2017-08-21T10:34:24.703

Reputation: 311

0

Microscript II, 14 bytes

$s{o`s`+s}sNv*

SuperJedi224

Posted 2017-08-21T10:34:24.703

Reputation: 11 342

0

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

LyricLy

Posted 2017-08-21T10:34:24.703

Reputation: 3 313

0

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.

user50198

Posted 2017-08-21T10:34:24.703

Reputation:

0

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

RosLuP

Posted 2017-08-21T10:34:24.703

Reputation: 3 036

0

Ruby, 12 bytes

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

13 bytes

Array.new n,n

marmeladze

Posted 2017-08-21T10:34:24.703

Reputation: 227

0

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'

steenbergh

Posted 2017-08-21T10:34:24.703

Reputation: 7 772

0

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.

Thor

Posted 2017-08-21T10:34:24.703

Reputation: 2 526

1This 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. – ბიმო – 2018-01-30T20:08:37.093

@BMO: converting to a list is trivial: echo -n '['; echo 5 | dc dcscript | paste -sd, | tr -d \\n ; echo ']' – Thor – 2018-01-30T21:19:32.977

That would make it a bash submission though. – ბიმო – 2018-01-30T22:53:35.057

0

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)])

Taylor Scott

Posted 2017-08-21T10:34:24.703

Reputation: 6 709

0

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.

Giuseppe

Posted 2017-08-21T10:34:24.703

Reputation: 21 077

0

///, 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.

Comrade SparklePony

Posted 2017-08-21T10:34:24.703

Reputation: 5 784

0

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)

Mercury Platinum

Posted 2017-08-21T10:34:24.703

Reputation: 161

Thanks, although I am still trying to figure out how I could get rid of the variables I needed to set – Mercury Platinum – 2018-01-31T18:18:27.133

You should post that as your answer, not going to copy it. – Mercury Platinum – 2018-01-31T18:26:56.910

That's up to you :) However I already have my own answer.

– Mr. Xcoder – 2018-01-31T18:28:34.253

0

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.

rafa11111

Posted 2017-08-21T10:34:24.703

Reputation: 310

0

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

streetster

Posted 2017-08-21T10:34:24.703

Reputation: 3 635

0

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

Weijun Zhou

Posted 2017-08-21T10:34:24.703

Reputation: 3 396

1y* does it without using the codepoint exemption. y is the first line of standard input by default. – recursive – 2018-03-26T23:06:22.483

True, but "The elements need to be integers, not strings". – Weijun Zhou – 2018-03-27T01:21:37.643

0

12-basic, 17 14 bytes

?[X=INPUT()]*X

Try

12Me21

Posted 2017-08-21T10:34:24.703

Reputation: 6 110

0

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.

snail_

Posted 2017-08-21T10:34:24.703

Reputation: 1 982

15 – ASCII-only – 2019-02-22T11:19:29.087

0

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.

Oliver

Posted 2017-08-21T10:34:24.703

Reputation: 7 160

0

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!

KrystosTheOverlord

Posted 2017-08-21T10:34:24.703

Reputation: 681

lol, there's already a 5 byte one – ASCII-only – 2019-02-22T11:25:00.670

@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 – 2019-02-22T22:06:24.450

Yeah well that's just the difference between a function and a program :P – ASCII-only – 2019-02-22T22:53:03.050