Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a nested recursive list without slicing

I need to write a function that receives an non-negative integer and returns:

[] for n=0 

[[]] for n=1 

[[],[[]]] for n=2

[[],[[]],[[],[[]]]] for n=3

And so on. For n, we will receive an n sized list, so that in index i there will be all the i-1 elements from the list. I don't know how to explain that better, English isn't my first language.

I'm not allowed to use list slicing or loops and I'm supposed to create deep copies of each list, without the copy module. I'm not allowed to let 2 different lists or indexes point to the same list in memory.

This is what I tried:

def list_seq(x, outer_list=[]):
    if x == 0:
        return []
    outer_list.append(list_seq(x-1,outer_list))
    return outer_list

And the output for print(list_seq(2)) is [[], [...]].

like image 636
MadaBit Avatar asked Nov 27 '21 14:11

MadaBit


People also ask

How to flatten a nested list using recursion in Python?

Given a nested list, the task is to write a python program to flatten a nested list using recursion. Firstly, we try to initialize a variable into the linked list. Then, our next task is to pass our list as an argument to a recursive function for flattening the list.

How to flatten a linked list using recursion?

Step-by-step Approach: 1 Firstly, we try to initialize a variable into the linked list. 2 Then, our next task is to pass our list as an argument to a recursive function for flattening the list. 3 In that recursive function, if we find the list as empty then we return the list. More items...

How to create and remove a nested list in Java?

1 Create a Nested List. A nested list is created by placing a comma-separated sequence of sublists. ... 2 Add items to a Nested list. To add new values to the end of the nested list, use append () method. ... 3 Remove items from a Nested List. If you know the index of the item you want, you can use pop () method. ... 4 Find Nested List Length. ...

How to create a nested list in SQL?

A nested list is created by placing a comma-separated sequence of sublists. You can access individual items in a nested list using multiple indexes. The indexes for the items in a nested list are illustrated as below: You can access a nested list by negative indexing as well. Negative indexes count backward from the end of the list.


Video Answer


4 Answers

If you can't use loops, you can use the following:

def recursive_list(n):
    if n == 0:
        return []
    else:
        return recursive_list(n-1) +  [recursive_list(n-1)]

EDIT

You can do the following if you want to use append:

def recursive_list(n: int) -> list:
    if n:
        result = recursive_list(n-1)
        result.append(recursive_list(n-1))
        return result
    return []

NOTE as pointed out in the comments, caching introduces some reference issues, so I have removed the cached versions.

like image 73
Sayandip Dutta Avatar answered Oct 17 '22 06:10

Sayandip Dutta


You can write this down as a recursive function using a simple list comprehension:

def f(n):
    return [f(i) for i in range(n)]

Or instead of the list comprehension, you could also use map:

def f(n):
    return list(map(f, range(n)))

Note, though, that without caching this is going to get rather slow rather quickly.

like image 3
tobias_k Avatar answered Oct 17 '22 06:10

tobias_k


An alternative shorter recursive solution, no loops:

def l_list(n):
  def f(c, d = []):
     return d if c == n else f(c+1, d+[l_list(c)])
  return f(0)

print(l_list(0))
print(l_list(1))
print(l_list(2))
print(l_list(3))

Output:

[]
[[]]
[[], [[]]]
[[], [[]], [[], [[]]]]
like image 1
Ajax1234 Avatar answered Oct 17 '22 07:10

Ajax1234


Just another idea, I think it fulfills all rules/requirements:

def f(n):
    a = []
    exec('a.append(1 * a);' * n)
    return eval(repr(a))

Demo usage:

for n in range(5):
    print(f(n))

Output:

[]
[[]]
[[], [[]]]
[[], [[]], [[], [[]]]]
[[], [[]], [[], [[]]], [[], [[]], [[], [[]]]]]
like image 1
no comment Avatar answered Oct 17 '22 07:10

no comment