Count Up, Replace, Repeat!

18

2

Definition

Define the nth array of the CURR sequence as follows.

  1. Begin with the singleton array A = [n].

  2. For each integer k in A, replace the entry k with k natural numbers, counting up from 1 to k.

  3. Repeat the previous step n - 1 more times.

For example, if n = 3, we start with the array [3].

We replace 3 with 1, 2, 3, yielding [1, 2, 3].

We now replace 1, 2, and 3 with 1; 1, 2 and 1, 2, 3 (resp.), yielding [1, 1, 2, 1, 2, 3].

Finally, we perform the same replacements as in the previous step for all six integers in the array, yielding [1, 1, 1, 2, 1, 1, 2, 1, 2, 3]. This is the third CURR array.

Task

Write a program of a function that, given a strictly positive integer n as input, computes the nth CURR array.

The output has to be a flat list of some kind (and array returned from a function, a string representation of your language's array syntax, whitespace-separated, etc.).

This is . May the shortest code in bytes win!

Test cases

 1 -> [1]
 2 -> [1, 1, 2]
 3 -> [1, 1, 1, 2, 1, 1, 2, 1, 2, 3]
 4 -> [1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4]
 5 -> [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5]
 6 -> [1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 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, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]

Dennis

Posted 2016-06-21T07:58:41.370

Reputation: 196 637

3

Related: Count, Replace, Add Up! ._.

– Dennis – 2016-06-21T07:58:47.717

Can we take the input as a singleton array (like [2]) rather than an integer? – Mego – 2016-06-21T08:13:52.937

@Mego Let's keep it at integers. – Dennis – 2016-06-21T08:17:37.430

I feel like there should be an OEIS sequence for this. – DanTheMan – 2016-06-21T16:31:11.280

@DanTheMan It's not really an integer sequence in its current form, but I guess it could be turned into one by concatenating the results for all positive integers. – Dennis – 2016-06-21T16:53:51.520

@Dennis There are a lot of sequences that are made by reading the rows of a table, and I think this would count as a table. – DanTheMan – 2016-06-21T16:55:50.763

Answers

23

Jelly, 3 bytes

R¡F

Try it online

Explanation

R¡F    Argument n

R      Yield range [1..n]
 ¡     Repeat n times
  F    Flatten the result

Essari

Posted 2016-06-21T07:58:41.370

Reputation: 541

That is... simply brilliant... compared to my Jelly answer. – Leaky Nun – 2016-06-21T09:45:23.137

6Great first post! – Blue – 2016-06-21T10:18:58.350

16

Python, 50 bytes

lambda i:eval("[i "+"for i in range(1,i+1)"*i+"]")

Scope abuse! For example, for i=3, the string to be evaluated expands to.

[i for i in range(1,i+1)for i in range(1,i+1)for i in range(1,i+1)]

Somehow, despite using the function input variable i for everything, Python distinguishes each iteration index as belonging to a separate scope as if the expression were

[l for j in range(1,i+1)for k in range(1,j+1)for l in range(1,k+1)]

with i the input to the function.

xnor

Posted 2016-06-21T07:58:41.370

Reputation: 115 687

This trick works in Firefox 30+ too and saved me 3 bytes, thanks! – Neil – 2016-06-21T15:28:59.653

@DigitalTrauma Both python and JavaScript have eval, obviously the code itself needs to be ported but I thought you could assume that anyway. – Neil – 2016-06-22T07:45:08.420

@Neil Oh, I see - I completely misunderstood :) – Digital Trauma – 2016-06-22T15:10:26.550

12

05AB1E, 6 3 bytes

DFL

Explained

D     # duplicate input
 F    # input times do
  L   # range(1,N)

Try it online

Saved 3 bytes thanks to @Adnan

Emigna

Posted 2016-06-21T07:58:41.370

Reputation: 50 798

DFL is 3 bytes shorter :) – Adnan – 2016-06-21T09:39:26.580

1@Adnan: Didn't know L worked like that on lists. A bit surprising that it flattens automatically. – Emigna – 2016-06-21T09:58:06.823

3It's actually a bug which I've never fixed :p. – Adnan – 2016-06-21T10:06:04.557

6

Retina, 33 bytes

$
$.`$*0
+%(M!&`1.*(?=0)|^.+
O`.+

Input and output in unary.

Try it online!

Even though I didn't use the closed form for the related challenge, adapting this answer was surprisingly tricky.

Martin Ender

Posted 2016-06-21T07:58:41.370

Reputation: 184 808

+%(M!& is the longest tag that I would ever see. – Leaky Nun – 2016-06-21T08:06:08.573

6

Python 2, 82 bytes

lambda n:[1+bin(i)[::-1].find('1')for i in range(1<<2*n-1)if bin(i).count('1')==n]

This isn't the shortest solution, but it illustrates an interesting method:

  • Write down the first 2^(2*n-1) numbers in binary
  • Keep those with exactly n ones
  • For each number, count the number of trailing zeroes, and add 1.

xnor

Posted 2016-06-21T07:58:41.370

Reputation: 115 687

4

Actually, 9 bytes

;#@`♂RΣ`n

Try it online!

Explanation:

;#@`♂RΣ`n
;#@        dupe n, make a singleton list, swap with n
   `♂RΣ`n  call the following function n times:
    ♂R       range(1, k+1) for k in list
      Σ      concatenate the ranges

Thanks to Leaky Nun for a byte, and inspiration for another 2 bytes.

Mego

Posted 2016-06-21T07:58:41.370

Reputation: 32 998

;#@"♂R♂i"*ƒ saves a byte – Leaky Nun – 2016-06-21T08:19:10.677

@LeakyNun Good catch - ;#@\♂R♂i`n` saves another one! – Mego – 2016-06-21T08:28:13.107

I was about to try summation, lol. – Leaky Nun – 2016-06-21T08:31:26.173

I think 9 is going to be the optimal solution here – Mego – 2016-06-21T08:35:41.787

Your link is still outdated. – Leaky Nun – 2016-06-21T08:39:44.553

4

C#, 128 Bytes

List<int>j(int n){var l=new List<int>(){n};for(;n>0;n--)l=l.Select(p=>Enumerable.Range(1,p)).SelectMany(m=>m).ToList();return l;

ScifiDeath

Posted 2016-06-21T07:58:41.370

Reputation: 151

With using static System.Linq.Enumerable, you can do this: int[]J(int n){var l=new[]{n};while (n-- > 0){l = l.Select(p => Range(1, p)).SelectMany(m => m).ToArray();}return l;} – die maus – 2016-06-21T20:13:44.853

4

APL, 11 bytes

{∊⍳¨∘∊⍣⍵+⍵}

Test:

      {∊⍳¨∘∊⍣⍵+⍵} 3
1 1 1 2 1 1 2 1 2 3

Explanation:

  • +⍵: starting with ,
  • ⍣⍵: do the following times:
    • ⍳¨∘∊: flatten the input, and then generate a list [1..N] for each N in the input
  • : flatten the result of that

marinus

Posted 2016-06-21T07:58:41.370

Reputation: 30 224

2Simpler: {(∊⍳¨)⍣⍵⊢⍵} – Adám – 2016-06-21T20:07:54.117

@Adám: Ah, yes, trains work differently from J. I'd started out with {(∊∘(⍳¨))⍣⍵+⍵} and then thought, how can I get rid of those braces? – marinus – 2016-06-23T14:16:07.050

2

CJam, 14 bytes

{_a\{:,:~:)}*}

Test it here.

Explanation

_a   e# Duplicate N and wrap it in an array.
\    e# Swap with other copy of N.
{    e# Do this N times...
  :, e#   Turn each x into [0 1 ... x-1].
  :~ e#   Unwrap each of those arrays.
  :) e#   Increment each element.
}*

Martin Ender

Posted 2016-06-21T07:58:41.370

Reputation: 184 808

2

Haskell, 33 bytes

f n=iterate(>>= \a->[1..a])[n]!!n

Thanks to nimi for saving a byte.

A pointfree version is longer (35 bytes):

(!!)=<<iterate(>>= \a->[1..a]).pure

xnor

Posted 2016-06-21T07:58:41.370

Reputation: 115 687

iterate(>>= \a->[1..a]) for one byte less. – nimi – 2016-06-21T15:18:57.850

2

Mathematica, 27 26 bytes

1 byte saved with some inspiration from Essari's answer.

Flatten@Nest[Range,{#},#]&

Fairly straightforward: for input x we start with {x} and then apply the Range to it x times (Range is Listable which means that it automatically applies to the integers inside arbitrarily nested lists). At the end Flatten the result.

Martin Ender

Posted 2016-06-21T07:58:41.370

Reputation: 184 808

2

Python 3, 75 74 bytes

def f(k):N=[k];exec('A=N;N=[]\nfor i in A:N+=range(1,i+1)\n'*k+'print(N)')

This is just a straightforward translation of the problem description to code.

Edit: Saved one byte thanks to @Dennis.

Andrew Epstein

Posted 2016-06-21T07:58:41.370

Reputation: 341

Your print can go outside the exec. – xnor – 2016-06-21T08:42:17.357

Yeah, that's what I had at first, but it simply prints [k] for some reason. I gave up trying to figure out if it was a scope issue or something else. – Andrew Epstein – 2016-06-21T08:44:46.357

Yes, it looks like a scope issue. It works fine in Python 2.

– xnor – 2016-06-21T08:48:12.920

2

JavaScript (Firefox 30-57), 63 60 bytes

f=n=>eval(`[${`for(n of Array(n+1).keys())`.repeat(n--)}n+1]`)

Port of @xnor's Python answer.

Neil

Posted 2016-06-21T07:58:41.370

Reputation: 95 035

I tried this with Firefox 42 (SyntaxError: missing : in conditional expression ) and Babel (Unexpected token (1:21)). What am I doing wrong? – Dennis – 2016-06-21T15:37:28.853

@Dennis Sorry, but I have no idea; I do in fact have Firefox 42 on one of my machines for some reason and I double-checked and it ran fine there. (I also checked Firefox 37 and 47 just to be sure.) – Neil – 2016-06-21T18:24:48.497

Huh, the page didn't refresh and I tested your old version. The new one works just fine. – Dennis – 2016-06-21T18:27:56.537

@Dennis Ah, it looks like a stray ) crept into that version somehow. – Neil – 2016-06-21T18:29:40.737

2

R, 60 49 bytes

Pretty straightforward use of unlist and sapply.

y=x=scan();for(i in 1:x)y=unlist(sapply(y,seq));y

Thanks to @MickyT for saving 11 bytes

bouncyball

Posted 2016-06-21T07:58:41.370

Reputation: 401

@MickyT thx for the tip, I can use seq to lower the byte count – bouncyball – 2016-06-22T12:48:03.070

Sorry I misread the question – MickyT – 2016-06-22T17:20:29.710

2

Clojure, 59 bytes

(fn[n](nth(iterate #(mapcat(fn[x](range 1(inc x)))%)[n])n))

Explanation:

Really straight forward way to solve the problem. Working from the inside out:

(1) (fn[x](range 1(inc x))) ;; return a list from 1 to x
(2) #(mapcat (1) %)         ;; map (1) over each item in list and flatten result
(3) (iterate (2) [n])       ;; call (2) repeatedly e.g. (f (f (f [n])))
(4) (nth (3) n))            ;; return the nth value of the iteration

mark

Posted 2016-06-21T07:58:41.370

Reputation: 251

2

php 121

Not really very much in the way of tricks behind this one. Flattening an array in php isn't short so it's necessary to build it flat in the first place

<?php for($a=[$b=$argv[1]];$b--;)$a=array_reduce($a,function($r,$v){return array_merge($r,range(1,$v));},[]);print_r($a);

user55641

Posted 2016-06-21T07:58:41.370

Reputation: 171

Keeping it flat is a good idea. But callback functions aren´t short either. Beat You by 15 bytes. You can save 4 bytes with the short tag <? or 6 bytes with -r and no tag. – Titus – 2016-12-06T19:01:34.287

1

Pyth, 8 bytes

usSMGQ]Q

Try it online!

usSMGQ]Q   input as Q

u    Q     repeat for Q times,
      ]Q   starting as [Q]:

  SMG          convert each number in the array to its range
 s             flatten

           then implicitly prints the result.

Leaky Nun

Posted 2016-06-21T07:58:41.370

Reputation: 45 011

1

Jelly, 7 bytes

Quick, before Dennis answers (jk)

WR€F$³¡

Try it online!

WR€F$³¡  Main monadic chain. Argument: z

W        Yield [z].
     ³¡  Repeat the following z times:
 R€          Convert each number in the array to the corresponding range.
   F         Flatten the array.

Leaky Nun

Posted 2016-06-21T07:58:41.370

Reputation: 45 011

1

J, 18 bytes

([:;<@(1+i.)"0)^:]

Straight-forward approach based on the process described in the challenge.

Usage

   f =: ([:;<@(1+i.)"0)^:]
   f 1
1
   f 2
1 1 2
   f 3
1 1 1 2 1 1 2 1 2 3
   f 4
1 1 1 1 2 1 1 1 2 1 1 2 1 2 3 1 1 1 2 1 1 2 1 2 3 1 1 2 1 2 3 1 2 3 4

Explanation

([:;<@(1+i.)"0)^:]  Input: n
                 ]  Identity function, gets the value n
(     ...     )^:   Repeat the following n times with an initial value [n]
      (    )"0        Means rank 0, or to operate on each atom in the list
         i.           Create a range from 0 to that value, exclusive
       1+             Add 1 to each to make the range from 1 to that value
    <@                Box the value
 [:;                  Combine the boxes and unbox them to make a list and return
                    Return the final result after n iterations

miles

Posted 2016-06-21T07:58:41.370

Reputation: 15 654

1

Python 2, 69 68 66 bytes

def f(n):a=[n];exec'a=sum([range(1,i+1)for i in a],[]);'*n;print a

Edit: Saved 1 byte thanks to @xnor. Saved 2 bytes thanks to @Dennis♦.

Neil

Posted 2016-06-21T07:58:41.370

Reputation: 95 035

You can remove the parens around exec. In Python 2, it's a keyword, not a function. I count 68 bytes btw. – Dennis – 2016-06-21T15:32:42.610

@Dennis Ah, that means I miscounted and it was originally 69 bytes... – Neil – 2016-06-21T18:26:10.533

1

F# , 63 bytes

fun n->Seq.fold(fun A _->List.collect(fun k->[1..k])A)[n]{1..n}

Returns an anonymous function taking n as input.

Replaces every entry k in A with [1..k], repeats the process n times, starting with A = [n].

hlo

Posted 2016-06-21T07:58:41.370

Reputation: 141

1

Swift 3, 58 Bytes

Meant to run directly in the a playground, with n set to the input:

var x=[n];for i in 0..<n{x=x.reduce([]){$0+[Int](1...$1)}}

Ungolfed, with most short hand notation reverted:

let n = 3 //input

var x: Array<Int> = [n]
for i in 0..<n {
    x = x.reduce(Array<Int>[], combine: { accumulator, element in
        accumulator + Array<Int>(1...element)
    })
}

Alexander - Reinstate Monica

Posted 2016-06-21T07:58:41.370

Reputation: 481

1

Java, 159 Bytes

Procedure

int[] q(int y){int z[]=new int[]{y};for(int i=0;i<y;i++){int d=0,a=0;for(int c:z)d+=c;int[]r=new int[d];for(int c:z)for(int j=0;j<c;)r[a++]=++j;z=r;}return z;}

Usage

public static void main(String[] args){String out = "["; int [] b = q(6);for(int c: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, 1, 2, 3, 4, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 1, 1, 2, 1, 1, 2, 1, 2, 3, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, ]

Giacomo Garabello

Posted 2016-06-21T07:58:41.370

Reputation: 1 419

1

Bash + GNU utilities, 49

  • 1 byte saved thanks to @Dennis.

Piped recursive functions FTW!

f()((($1))&&xargs -l seq|f $[$1-1]||dd)
f $1<<<$1

n is passed on the command-line. Output is newline-separated.

The use of dd causes statistics to be sent to STDERR. I think this is OK, but if not, dd can be replaced with cat at a cost of 1 extra byte.

Digital Trauma

Posted 2016-06-21T07:58:41.370

Reputation: 64 644

1Extraneous output to STDERR is allowed by default. You can replace {...;} with (...) to save a byte. – Dennis – 2016-06-21T22:24:21.087

@Dennis yes, of course! Apparently you got this tip from me :)

– Digital Trauma – 2016-06-21T22:34:16.347

0

PHP, 100 98 bytes

Run with php -r '<code>' <n>.

for($a=[$n=$argv[1]];$n--;$a=$b)for($b=[],$k=0;$c=$a[$k++];)for($i=0;$i++<$c;)$b[]=$i;print_r($a);

In each iteration create a temporary copy looping from 1..(first value removed) until $a is empty.


These two are still and will probably remain at 100 bytes:

for($a=[$n=$argv[1]];$n--;)for($i=count($a);$i--;)array_splice($a,$i,1,range(1,$a[$i]));print_r($a);

In each iteration loop backwards through array replacing each number with a range.

for($a=[$n=$argv[1]];$n--;)for($i=$c=0;$c=$a[$i+=$c];)array_splice($a,$i,1,range(1,$c));print_r($a);

In each iteration loop through array increasing index by previous number and replacing each indexed element with a range

Titus

Posted 2016-06-21T07:58:41.370

Reputation: 13 814

0

Perl 5, 53 bytes

A subroutine:

{($i)=@_;for(1..$i){my@c;push@c,1..$_ for@_;@_=@c}@_}

See it in action as

perl -e'print "$_ " for sub{($i)=@_;for(1..$i){my@c;push@c,1..$_ for@_;@_=@c}@_}->(3)'

msh210

Posted 2016-06-21T07:58:41.370

Reputation: 3 094

0

Ruby, 61 bytes

def f(n);a=[n];n.times{a=a.map{|i|(1..i).to_a}.flatten};a;end

Andrew

Posted 2016-06-21T07:58:41.370

Reputation: 131

Hi! Welcome to PPCG. A short explanation would be nice! – TheCoffeeCup – 2016-06-23T02:41:09.400

Welcome to PPCG! Unless the challenge overrules this all submissions need to be full programs or functions, not just snippets. (And in this case, the challenge actually confirms this requirement.)

– Martin Ender – 2016-06-27T10:02:06.170