Home on the Range of Lists

26

This challenge is simply to return a list of lists of integers, similar to the Python range function, except that each successive number must be that deep in lists.

Rules:

  • Create a program or a non-anonymous function
  • It should return or print the result
  • The result should be returned in a list (of lists) or array (of arrays)
  • If the parameter is zero, return an empty list
  • This should be able to handle an integer parameter 0 <= n < 70.
    • (recursive solutions blow up pretty fast)
  • The function should be callable with only the one parameter.
  • Other behavior is undefined.
  • This is code golf, so shortest code wins.

Example Call:

rangeList(6)
> [0, [1, [2, [3, [4, [5]]]]]]

Test Cases:

0  => []
1  => [0]
2  => [0, [1]]
6  => [0, [1, [2, [3, [4, [5]]]]]]
26 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25]]]]]]]]]]]]]]]]]]]]]]]]]]
69 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

EDIT: isaacg's answer is the shortest so far. I'll update the accepted answer if anyone finds a shorter one in a language that existed at the posting of the challenge. Thanks for playing!

mbomb007

Posted 2015-03-04T15:25:51.267

Reputation: 21 944

2Random comment: It's funny how the character minimum for a title is 15, and I couldn't use "Range of Lists", so I came up with this one on the spot. – mbomb007 – 2015-03-04T15:48:05.107

That's mostly to prevent people from writing unassigned anonymous functions. Personally, I'd prefer it if it was a function that takes a parameter. – mbomb007 – 2015-03-04T16:20:06.837

Is it allowed to create two functions, where one is a helper function? – ProgramFOX – 2015-03-04T18:58:18.473

@ProgramFOX Yes. I think code external to your function is fine, since if someone wanted to import math in Python for example, I don't think it could occur inside a function. – mbomb007 – 2015-03-04T19:42:52.740

@DevonParsons There are plenty of questions that have an example program contained within, but okay. – mbomb007 – 2015-03-04T20:21:34.533

Are tuples okay? – wchargin – 2015-03-06T04:14:07.347

@WChargin I'm going to say "no." Tuples are immutable, and I'm going to say that mutability is important. – mbomb007 – 2015-03-06T19:54:28.513

Answers

11

Pyth, 13 bytes

?hu]+HG_UQYQY

Try it here.

                 Implicit:
                 Q = eval(input())
                 Y = []
?           QY   If Q = 0, print Y
 h               else, print the first element of
  u     _UQY     the reduce, where Y is the initial value, over the list
                 reversed(range(Q))
   ]+HG          The reduce function: The list containing H prepended onto G.
                 The new number is inserted into the accumulated list,
                 then the resultant list is wrapped in another list.

isaacg

Posted 2015-03-04T15:25:51.267

Reputation: 39 268

10

APL (13 18)

Assuming ⎕IO=0:

f←{×⍵:⊃,∘⊂∘,/⍳⍵⋄⍬}

Explanation:

  • ×⍵: if is positive,
    • ,∘⊂∘,: join the left operand to the enclose of the right operand (i.e. x ,∘⊂∘, y = [x, [y]])
    • /: reduce
    • ⍳⍵: the numbers 0..⍵-1
    • : disclose the result
  • : otherwise
    • : return the empty list
    • (this is necessary because / fails on , and ⍳0 gives the empty list.)

Addendum:

This function returns a nested array. However, it is a bit hard to tell this from APL's default output. It separates array items by spaces, so you can only tell nesting by double spaces. Here is a function that will take a nested array, and return a string, formatting the nested array in Python style (i.e. [a,[b,[c,...]]]).

arrfmt←{0=≡⍵:⍕⍵ ⋄ '[',(1↓∊',',¨∇¨⍵),']'}

marinus

Posted 2015-03-04T15:25:51.267

Reputation: 30 224

1I think you need another ∘, after the enclose, otherwise (at least in my interpreter - dyalog14) the last element isn't enclosed. e.g. [0 [1 [2 3]]] – Moris Zucca – 2015-03-05T15:16:01.933

@marinus Can you please verify this? – mbomb007 – 2015-03-06T19:48:55.467

I changed the problem statement a day or two ago to clarify that defined functions should be assigned to a variable. You should add f← to the start of your program unless you modify it to accept user input. – mbomb007 – 2015-03-06T21:24:24.160

Also, the output doesn't show how deep in the list a number is vary clearly... is each space an implied bracket? – mbomb007 – 2015-03-06T21:25:29.517

@MorisZucca I have to agree. See here: http://ngn.github.io/apl/web/#code=%7B%D7%u2375%3A%2C%u2218%u2282/%u2373%u2375%u22C4%u236C%7D%203%0A

– mbomb007 – 2015-03-06T21:28:26.103

@mbomb007 to check how deep the number is in the list, you can try the function in tryapl.org, adding a "]display " in front. – Moris Zucca – 2015-03-07T08:05:10.617

@mbomb007: it encloses properly now, and it is no longer anonymous. APL's default output format for nested lists is indeed not very intuitive, but it returns a nested list nonetheless, which you can check by indexing into the lists. On TryAPL you will get a wrong answer (1..N instead of 0..N-1) because it assumes ⎕IO=1 and doesn't allow you to change it. ngn/apl works. – marinus – 2015-03-07T17:57:32.840

@marinus I've just tried and ⎕IO←0 works on tryapl.org, so your function works there as well. – Moris Zucca – 2015-03-07T18:11:54.493

@MorisZucca: ah, so they fixed that. – marinus – 2015-03-07T18:14:30.900

9

Haskell, 67 bytes

data L=E|I Int|L[L] 
1#m=L[I$m-1]
n#m=L[I$m-n,(n-1)#m]
p 0=E
p n=n#n

In Haskell all elements of a list have to be of the same type, so I cannot mix integers with list of integers and I have to define a custom list type L. The helper function # recursively constructs the required list. The main function p checks for the empty list and calls # otherwise.

As new data types cannot be printed by default (the rules allow just to return the list), I add some more code for demonstration purpose:

data L=E|I Int|L[L] deriving Show

Now:

-- mapM_ (print . p) [0..5]
E
L [I 0]
L [I 0,L [I 1]]
L [I 0,L [I 1,L [I 2]]]
L [I 0,L [I 1,L [I 2,L [I 3]]]]
L [I 0,L [I 1,L [I 2,L [I 3,L [I 4]]]]]

nimi

Posted 2015-03-04T15:25:51.267

Reputation: 34 639

7

Python, 48 bytes

f=lambda n,i=0:i<n and[i]+[f(n,i+1)]*(i<n-1)or[]

Using list multiplication to handle the special case.

Sp3000

Posted 2015-03-04T15:25:51.267

Reputation: 58 729

I don't think this is Python 2 specific - seems to work in all pythons. – isaacg – 2015-03-04T22:24:55.883

@isaacg Fixed. My original submission wasn't though :) – Sp3000 – 2015-03-04T23:23:00.037

A tiny char-save:*(i<n-1) can be done as [:n+~i], since it's a singleton list. – xnor – 2015-03-05T00:01:08.423

6

Mathematica, 33

f@0={};f@1={0};f@n_:={0,f[n-1]+1}

alephalpha

Posted 2015-03-04T15:25:51.267

Reputation: 23 988

It looks so simple! – mbomb007 – 2015-03-04T20:08:33.903

5

CJam, 16 bytes

Lri){[}%]~;']*~p

This is a full program. It takes input via STDIN and prints the final array on to STDOUT.

As with the other CJam entry, 0 input will print "" as that is the representation of an empty array in CJam.

How it works:

L                   "Put an empty array on stack. This will be used for the 0 input";
 ri)                "Read the input, convert it to integer and increment it";
    {[}%            "Map over the array [0 ... input number] starting another array";
                    "after each element";
        ]~;         "Now on stack, we have input number, an empty array and the final";
                    "opening bracket. Close that array, unwrap it and pop the empty array";
           ']*~     "Put a string containing input number of ] characters and eval it";
                    "This closes all the opened arrays in the map earlier";
               p    "Print the string representation of the array";
                    "If the input was 0, the map runs 1 time and the ; pops that 1 array";
                    "Thus leaving only the initial empty array on stack";

Try it online here

Optimizer

Posted 2015-03-04T15:25:51.267

Reputation: 25 836

3

Java, 88 107 105 104 102 bytes

import java.util.*;int o;List f(final int n){return new Stack(){{add(n<1?"":o++);if(o<n)add(f(n));}};}

Pretty long compared to the others, though you can't do much better with Java. A check to determine whether to continue recursion is all it takes.

TNT

Posted 2015-03-04T15:25:51.267

Reputation: 2 442

You need to import java.util.*; for this to be self-containing (or fully qualify java.util.List and java.util.Stack, but that's much longer). +19 to make it 107, still 7 better than the Java answer I was working on :D – Geobits – 2015-03-04T17:07:34.653

I see two you can save: o!=n can be o<n, and you can swap the ternary to o<n?o++:"". – Geobits – 2015-03-04T18:01:14.027

In Java 8, I believe the final on int n can be removed. – Justin – 2015-03-05T17:57:08.260

3

JavaScript (ES6) 40

Recursive solution, pretty robust, no blows. Update Fails near 6500 with 'too much recursion'

F=n=>n--?(R=m=>m<n?[m,R(++m)]:[m])(0):[]

Iterative solution (45) No limits except memory usage

F=n=>{for(s=n?[--n]:[];n;)s=[--n,s];return s}

Try F(1000): FireBug console will not show you more than 190 nested arrays, but they are there

edc65

Posted 2015-03-04T15:25:51.267

Reputation: 31 086

2

Haskell, 65 59 45 41 bytes

These nested lists are the same data-structure as rooted Trees, except that they can also be empty. Hence, we can use a list of them - also called a Forest to represent them.

(0!)
data T=N[T]Int
m!n=[N((m+1)!n)m|m<n]

Try it online!

Explanation

First of all we need to implement the Tree data type:

data Tree = Node [Tree] Int

From there it's just recursion using two parameters m (counting up) and n to keep track when to terminate:

m ! n= [ Node ((m+1)!n) m| m<n ]

Alternative, 61 bytes

import Data.Tree
f n=unfoldForest(\b->(b,[b+1|b<n-1]))[0|n>0]

Try it online!

Explanation

The function unfoldForest takes a list of initial values and a function x -> (y,[x]). For each initial value x it unfolds a tree using the function, producing a tuple (y,xs) where y will become the root and the xs are used to repeat the procedure:

unfoldForest (\b -> (b, [b+1 | b < 2]) [0]
  ≡ Node 0 [unfoldForest (\b -> (b, [b+1 | b < 2) [1]]
  ≡ Node 0 [Node 1 [unfoldForest (\b -> (b, [b+1 | b < 2) []]]
  ≡ Node 0 [Node 1 []]

ბიმო

Posted 2015-03-04T15:25:51.267

Reputation: 15 345

2

C# - 100

Simple recursion. Check the zero special case and tick up with one variable, down with the other

object[]A(int y,int x=0){return y==0?new object[0]:y==1?new object[]{x}:new object[]{x,A(--y,++x)};}

C++ 87

(Visual C++ 2012)

int*A(int y,int x=0){int*b=new int{x};return!y?new int:--y?(b[1]=(int)A(y,++x))?b:0:b;}

This one is great, by which I mean byzantine, but it's the same basic idea as the c# one.

It's a C style array implementation, so it doesn't give you an array, it gives a int pointer, in which I was be storing both ints and other pointers. Like this: [0,*] *->[1,#] #-> [2,&] &-> etc, where the symbols are pseudo code for the int value of a pointer and the --> is where it pointers to in memory.

What an excellent easy to use implementation of c style jagged arrays I've devised (cough), but I maintain it's plausible enough to be within the rules of the question.

There's quite a lot of abusing ternary operators here, and also quite a lot of abusing the implicit cast from int to bool.

Example: If we let int *bar = (int*)A(3);, we can see:

bar
0x003bded8 {0}
((int*)bar[1])[0]
1
((int*)(((int*)bar[1])[1]))[0]
2

Which is pointer talk for [0,[1,[2]]].

Okay, fine. It doesn't actually have to be the awful. Here's some test code for running this c++ code:

int* GetNext(int* p){
  return (int*)p[1];
}

int main()
{
    auto x = 10;
    auto bar = A(x);

    for (int i = 1; i < x; i++){
        bar = GetNext(bar);
        std::cout << bar[0] << std::endl;
    }

}

Nathan Cooper

Posted 2015-03-04T15:25:51.267

Reputation: 617

The C++ version doesn't compile. http://ideone.com/fmcXYP

– Anmol Singh Jaggi – 2015-03-06T10:27:04.233

You should mention the compiler used alongside C++. – Anmol Singh Jaggi – 2015-03-07T04:59:58.733

@anmolSinghJaggi Yeah, good idea. Visual C++ 2012, which is mostly C++11 compliant. – Nathan Cooper – 2015-03-07T10:29:51.050

Old post, but with some tinkering I got it down to 86. Array g(params object[]a)=>a;Array f(int y,int x=0)=>y<1?g():y<2?g(x):g(x,f(y-1,x+1));

– dana – 2019-01-15T23:02:31.460

2

Python 2, 56 bytes

I suspect this could be golfed more.

f=lambda n,i=0:[i,f(n,i+1)]if i<n-1 else[i]if n>0 else[]

Tests:

# for n in (0,1,2,6,26,69): print n, '=>', f(n)
0 => []
1 => [0]
2 => [0, [1]]
6 => [0, [1, [2, [3, [4, [5]]]]]]
26 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25]]]]]]]]]]]]]]]]]]]]]]]]]]
69 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

Logic Knight

Posted 2015-03-04T15:25:51.267

Reputation: 6 622

Well, you beat my Python solution. – mbomb007 – 2015-03-04T16:02:27.770

2

Pyth, 15 bytes

?u[HG)_UtQ]tQQY

Which is really saying, in Python:

Q = eval(input())
if Q:
    print reduce(lambda G,H:[H,G], reverse(range(Q-1)), [Q-1])
else:
    print []

swstephe

Posted 2015-03-04T15:25:51.267

Reputation: 649

Hey, I'm glad you're learning Pyth! If you want to generate the evaluated input, you can use Q, which does that for you. Also, Y is preinitialized to []. – isaacg – 2015-03-04T16:53:20.650

qJ_1 is the same as !Q. And JtQ actually wastes 1 byte. ?Y!Qu[HG)_UtQ[tQ – Jakube – 2015-03-05T00:17:25.100

Okay I'll take those bytes. – swstephe – 2015-03-05T16:06:53.897

@swstephe If you change [tQ to ]tQ, which is equivalent, you swap to order of operations of ?, so you can replace !Q with Q. This results in ?u[HG)_UtQ]tQQY - 1 more byte saved. – isaacg – 2015-03-08T08:55:02.813

2

CJam, 17 bytes

I know Optimizer has found 16, but here is the best I can do:

{:I{[}%;{]}I1e>*}

This is a block, the closest thing to a function in CJam, which takes an integer on the stack, and leaves the desired nested array.

Use this program to test it, which puts the input on the stack, then calls the function and inspects the stack. Note that for 0, the stack output will contain "" - this is CJam's native representation of an empty array.

Martin Ender

Posted 2015-03-04T15:25:51.267

Reputation: 184 808

2

Ruby 46

f=->n{r=a=[]
(0...n).map{|i|a<<a=[i]}
r[0]||r}

Test it online: http://ideone.com/uYRVTa

Cristian Lupascu

Posted 2015-03-04T15:25:51.267

Reputation: 8 369

1

Perl - 44

sub t{$r=[($t)=@_];$r=[$t,$r]while--$t>0;$r}

Will add explanation upon request. You can try it here.

hmatt1

Posted 2015-03-04T15:25:51.267

Reputation: 3 356

I'm wondering, because I'm not familiar with Perl - Does the most deeply nested array have 2 elements in it, one being nil or whatever the equivalent is? I ask because on the page you link to the innermost array looks like (3,) – Devon Parsons – 2015-03-04T18:40:05.000

1@DevonParsons the code I added to print it in a readable way adds a comma after each element. undef is the equilvalent of nil or null in Perl and there is not an extra element. Perl flattens arrays so this is creating nested array references. – hmatt1 – 2015-03-04T18:45:00.837

1

Ruby - Recursive version - 52

r=->(n,v=nil){(n-=1;n<0 ?v:r[n,(v ?[n,v]:[n])])||[]}

Non-recursive version: 66 62 57

r=->i{(i-1).downto(0).inject(nil){|a,n|a ?[n,a]:[n]}||[]}

Sample output (same for both versions)

p r[0]  # => []
p r[1]  # => [0]
p r[2]  # => [0, [1]]
p r[6]  # => [0, [1, [2, [3, [4, [5]]]]]]
p r[26] # => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25]]]]]]]]]]]]]]]]]]]]]]]]]]
p r[69] # => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

The non-recursive version can handle arbitrarily large input.

p r[1000] # => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68, [69, [70, [71, [72, [73, [74, [75, [76, [77, [78, [79, [80, [81, [82, [83, [84, [85, [86, [87, [88, [89, [90, [91, [92, [93, [94, [95, [96, [97, [98, [99, [100, [101, [102, [103, [104, [105, [106, [107, [108, [109, [110, [111, [112, [113, [114, [115, [116, [117, [118, [119, [120, [121, [122, [123, [124, [125, [126, [127, [128, [129, [130, [131, [132, [133, [134, [135, [136, [137, [138, [139, [140, [141, [142, [143, [144, [145, [146, [147, [148, [149, [150, [151, [152, [153, [154, [155, [156, [157, [158, [159, [160, [161, [162, [163, [164, [165, [166, [167, [168, [169, [170, [171, [172, [173, [174, [175, [176, [177, [178, [179, [180, [181, [182, [183, [184, [185, [186, [187, [188, [189, [190, [191, [192, [193, [194, [195, [196, [197, [198, [199, [200, [201, [202, [203, [204, [205, [206, [207, [208, [209, [210, [211, [212, [213, [214, [215, [216, [217, [218, [219, [220, [221, [222, [223, [224, [225, [226, [227, [228, [229, [230, [231, [232, [233, [234, [235, [236, [237, [238, [239, [240, [241, [242, [243, [244, [245, [246, [247, [248, [249, [250, [251, [252, [253, [254, [255, [256, [257, [258, [259, [260, [261, [262, [263, [264, [265, [266, [267, [268, [269, [270, [271, [272, [273, [274, [275, [276, [277, [278, [279, [280, [281, [282, [283, [284, [285, [286, [287, [288, [289, [290, [291, [292, [293, [294, [295, [296, [297, [298, [299, [300, [301, [302, [303, [304, [305, [306, [307, [308, [309, [310, [311, [312, [313, [314, [315, [316, [317, [318, [319, [320, [321, [322, [323, [324, [325, [326, [327, [328, [329, [330, [331, [332, [333, [334, [335, [336, [337, [338, [339, [340, [341, [342, [343, [344, [345, [346, [347, [348, [349, [350, [351, [352, [353, [354, [355, [356, [357, [358, [359, [360, [361, [362, [363, [364, [365, [366, [367, [368, [369, [370, [371, [372, [373, [374, [375, [376, [377, [378, [379, [380, [381, [382, [383, [384, [385, [386, [387, [388, [389, [390, [391, [392, [393, [394, [395, [396, [397, [398, [399, [400, [401, [402, [403, [404, [405, [406, [407, [408, [409, [410, [411, [412, [413, [414, [415, [416, [417, [418, [419, [420, [421, [422, [423, [424, [425, [426, [427, [428, [429, [430, [431, [432, [433, [434, [435, [436, [437, [438, [439, [440, [441, [442, [443, [444, [445, [446, [447, [448, [449, [450, [451, [452, [453, [454, [455, [456, [457, [458, [459, [460, [461, [462, [463, [464, [465, [466, [467, [468, [469, [470, [471, [472, [473, [474, [475, [476, [477, [478, [479, [480, [481, [482, [483, [484, [485, [486, [487, [488, [489, [490, [491, [492, [493, [494, [495, [496, [497, [498, [499, [500, [501, [502, [503, [504, [505, [506, [507, [508, [509, [510, [511, [512, [513, [514, [515, [516, [517, [518, [519, [520, [521, [522, [523, [524, [525, [526, [527, [528, [529, [530, [531, [532, [533, [534, [535, [536, [537, [538, [539, [540, [541, [542, [543, [544, [545, [546, [547, [548, [549, [550, [551, [552, [553, [554, [555, [556, [557, [558, [559, [560, [561, [562, [563, [564, [565, [566, [567, [568, [569, [570, [571, [572, [573, [574, [575, [576, [577, [578, [579, [580, [581, [582, [583, [584, [585, [586, [587, [588, [589, [590, [591, [592, [593, [594, [595, [596, [597, [598, [599, [600, [601, [602, [603, [604, [605, [606, [607, [608, [609, [610, [611, [612, [613, [614, [615, [616, [617, [618, [619, [620, [621, [622, [623, [624, [625, [626, [627, [628, [629, [630, [631, [632, [633, [634, [635, [636, [637, [638, [639, [640, [641, [642, [643, [644, [645, [646, [647, [648, [649, [650, [651, [652, [653, [654, [655, [656, [657, [658, [659, [660, [661, [662, [663, [664, [665, [666, [667, [668, [669, [670, [671, [672, [673, [674, [675, [676, [677, [678, [679, [680, [681, [682, [683, [684, [685, [686, [687, [688, [689, [690, [691, [692, [693, [694, [695, [696, [697, [698, [699, [700, [701, [702, [703, [704, [705, [706, [707, [708, [709, [710, [711, [712, [713, [714, [715, [716, [717, [718, [719, [720, [721, [722, [723, [724, [725, [726, [727, [728, [729, [730, [731, [732, [733, [734, [735, [736, [737, [738, [739, [740, [741, [742, [743, [744, [745, [746, [747, [748, [749, [750, [751, [752, [753, [754, [755, [756, [757, [758, [759, [760, [761, [762, [763, [764, [765, [766, [767, [768, [769, [770, [771, [772, [773, [774, [775, [776, [777, [778, [779, [780, [781, [782, [783, [784, [785, [786, [787, [788, [789, [790, [791, [792, [793, [794, [795, [796, [797, [798, [799, [800, [801, [802, [803, [804, [805, [806, [807, [808, [809, [810, [811, [812, [813, [814, [815, [816, [817, [818, [819, [820, [821, [822, [823, [824, [825, [826, [827, [828, [829, [830, [831, [832, [833, [834, [835, [836, [837, [838, [839, [840, [841, [842, [843, [844, [845, [846, [847, [848, [849, [850, [851, [852, [853, [854, [855, [856, [857, [858, [859, [860, [861, [862, [863, [864, [865, [866, [867, [868, [869, [870, [871, [872, [873, [874, [875, [876, [877, [878, [879, [880, [881, [882, [883, [884, [885, [886, [887, [888, [889, [890, [891, [892, [893, [894, [895, [896, [897, [898, [899, [900, [901, [902, [903, [904, [905, [906, [907, [908, [909, [910, [911, [912, [913, [914, [915, [916, [917, [918, [919, [920, [921, [922, [923, [924, [925, [926, [927, [928, [929, [930, [931, [932, [933, [934, [935, [936, [937, [938, [939, [940, [941, [942, [943, [944, [945, [946, [947, [948, [949, [950, [951, [952, [953, [954, [955, [956, [957, [958, [959, [960, [961, [962, [963, [964, [965, [966, [967, [968, [969, [970, [971, [972, [973, [974, [975, [976, [977, [978, [979, [980, [981, [982, [983, [984, [985, [986, [987, [988, [989, [990, [991, [992, [993, [994, [995, [996, [997, [998, [999]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

Both versions also gracefully accept negative numbers

p r[-5] # => []

Devon Parsons

Posted 2015-03-04T15:25:51.267

Reputation: 173

Just curious, at what value does the recursive solution fail (due to memory/stack overflow)? – mbomb007 – 2015-03-04T19:44:16.047

@mbomb007 On Windows 7 x64, 16Gb RAM, it works on 926 and fails at 927 (stack level too deep (SystemStackError)) – Devon Parsons – 2015-03-04T19:48:07.540

1

JavaScript, 93 bytes

This isn't ideal, but I might as well give it a try. I'll try and golf this further later on, though for now I see no obvious way to.

function f(n){s='[';i=0;while(i<n-1)s+=i+++',[';s+=i||'';do{s+=']'}while(i--);return eval(s)}

vvye

Posted 2015-03-04T15:25:51.267

Reputation: 261

You might also try creating a recursive solution, as it might be shorter. – mbomb007 – 2015-03-04T19:45:48.537

1

Python, 75 bytes

This is just for show. It's the program I wrote when creating/designing this challenge.

f=lambda x,y=[]:y if x<1 else f(x-1,[x-2]+[y or[x-1]])if x>1 else y or[x-1]

mbomb007

Posted 2015-03-04T15:25:51.267

Reputation: 21 944

1

Python, 44

f=lambda n,i=0:i<n-1and[i,f(n,i+1)]or[i][:n]

Recursively creates the tree. The [:n] at the end is to special-case n==0 into giving the empty list.

xnor

Posted 2015-03-04T15:25:51.267

Reputation: 115 687

It was during this challenge that I realized that and and or can have spaces omitted next to integers, but else cannot. – mbomb007 – 2015-03-06T19:47:53.157

@mbomb007 It's because else starts with e, and things like 1e6 are valid number literals. – xnor – 2015-03-06T22:43:18.040

I knew that, but I didn't know that was why. Thanks. – mbomb007 – 2015-03-06T22:53:26.507

1@mbomb007 Actually after 2.6 or 2.7 or so you can lose the space before else, e.g. x = 1 if y==2else 5 works. – Sp3000 – 2015-03-15T02:20:05.383

It doesn't work in Python 2.7.2 http://repl.it/eB6 (But it does work in 3.4)

– mbomb007 – 2015-03-15T04:21:57.627

1

Joe, 8 bytes

Note: This is a non-competing answer. The first version of Joe was released after this question.

F:/+,M]R

What do we have here? F: defines a function F that is a chain of /+,, M] and R. When you call Fn, first Rn gets evaluated, returning range from 0 to n, exclusive. M] wraps each element to a list. Then the list is applied to /+,. x +, y returns x + [y]. / is a right fold. Thus, /+,a b c d... returns [a, [b, [c, [d...]]].

Example invocations (code is indented by 3, output by 0):

   F:/+,M]R
   F10
[0, [1, [2, [3, [4, [5, [6, [7, [8, [9]]]]]]]]]]
   F2
[0, [1]]
   F1
[0]
   F0
[]
   F_5
[0, [-1, [-2, [-3, [-4]]]]]

seequ

Posted 2015-03-04T15:25:51.267

Reputation: 1 714

0

JavaScript, 35 37 bytes

Recursive solution

f=(n,x=[])=>n?f(--n,x[0]?[n,x]:[n]):x

Try it online!

guest271314

Posted 2015-03-04T15:25:51.267

Reputation: 1

0

05AB1E, 11 bytes

_i¯ëFNI<α)R

Try it online or verify all test cases.

11-bytes alternative:

_i¯ëݨRvy)R

Try it online or verify all test cases.

Explanation:

05AB1E has no loops that go downwards, so to loop in the range (input, 0] I either have to:

  • Create that range first (ݨR; create range [0, input], remove last item, reverse), and then loop over it (vy);
  • Or loop in the range [0, input) instead (F), and take the absolute difference between the loop-index and the input-1 (NI<α).
_i          # If the (implicit) input is 0:
  ¯         #  Push the global array (empty by default)
 ë          # Else:
  F         #  Loop `N` in the range [0, (implicit) input):
   N        #   Push index `N`
    I<      #   Push input-1
      α     #   Take the absolute difference between the two
       )    #   Wrap everything on the stack into a list
        R   #   Reverse the list
            # (output the result implicitly after the if/loop)

Kevin Cruijssen

Posted 2015-03-04T15:25:51.267

Reputation: 67 575

0

PHP 5.4 (67 bytes):

I know, I know.

It's far from being the shortest answer.

But it works!

Here it is:

function F($n){for($c=$n?[--$n]:[];~$n&&$n--;$c=[$n,$c]);return$c;}

You can test it right here: https://ideone.com/42L35E (ignore the error)


Javascript (57 bytes):

This is the same exact code except that Javascript is picky about the return and I've reduced the variable names:

function F(n){for(c=n?[--n]:[];~n&&n--;c=[n,c]);return c}

See? Same code!


ES6 (49 bytes):

Basically the same exact code, but reduced for ES6:

F=n=>{for(c=n?[--n]:[];~n&&n--;c=[n,c]);return c}

Ismael Miguel

Posted 2015-03-04T15:25:51.267

Reputation: 6 797

I specified no anonymous functions, or it was in the comments somewhere. I'll make it clearer. – mbomb007 – 2015-03-05T18:05:31.693

@mbomb007 It wasn't specified. There was nothing enforcing a name on the function. But I've changed it. – Ismael Miguel – 2015-03-05T18:09:36.507

There was a comment by me below the question: "That's mostly to prevent people from writing unassigned anonymous functions. Personally, I'd prefer it if it was a function that takes a parameter." – mbomb007 – 2015-03-05T18:10:49.970

And I did change the question. But it's pretty standard codegolf for functions that they have to be callable by name (aka, more than once and without typing the entire function again.) That's why you see everyone else's functions using f=lambda... – mbomb007 – 2015-03-05T18:11:54.017

@mbomb007 But it's pretty standard codegolf for functions that they have to be callable by name (aka, more than once and without typing the entire function again.) --> never heard of this, and I use this website for somewhat close to a year. Besides, this is an invalid argument since you can assign the functions to a variable. – Ismael Miguel – 2015-03-05T18:13:42.693

But they must be assigned. That's my point. – mbomb007 – 2015-03-05T18:14:20.627

@mbomb007 No, they can be assigned, they don't must. Try !function(){alert('it runs')}() and try F=function(){alert('it runs')};F(); on your browser console. – Ismael Miguel – 2015-03-05T18:19:40.717

I declare that they must for this question. – mbomb007 – 2015-03-05T19:14:11.110

0

Javascript (114 bytes):

Everybody else was doing recursive, so I wanted to try an iterative solution. I have too many special cases, though.

I hold a master list, and then loop and append new lists with new numbers.

function q(n){a=[];if(n==1)a=[0];else if(n!=0){a=[0,b=[]];for(i=1;i<n;){c=[];b.push(c);b.push(i++);b=c}}return a}

Brian J

Posted 2015-03-04T15:25:51.267

Reputation: 653

0

Common Lisp (95 bytes):

(defun f(n &optional(i 0))(cond((< i(1- n))(cons i(list(f n(1+ i)))))((> n 0)(list i))(t nil)))

Mark Reed

Posted 2015-03-04T15:25:51.267

Reputation: 667