Home > Enterprise >  Returning a recursive output as a list
Returning a recursive output as a list

Time:11-28

It's my first question so I'm sorry for any mistakes.

I'm trying to find all combinations of K from N using recursion. and have this code:

   def k_size_subsets(n, k):
    list1 = [i for i in range(1, n   1)]
    data = [0] * k
    help_fun(n, k, data, list1,0,0)

def help_fun(n, k, data, list1,index, i):
    if index == k:
        more_help(data)
        return
    if i >= n:
        return
    data[index] = list1[i]
    help_fun(n, k, data, list1, index  1, i 1)
    help_fun(n, k, data, list1, index, i 1)

def more_help(list):
    str1 = ''
    for i in range(len(list)):
        str1  = str(list[i])
    print(str1)

k_size_subsets(5, 3)

The output I'm looking for is a list like this: ["123","124","125","134","135","145","234","235","245","345"]

so far the function outputs the right answer but as a string.

123 124 125 134 135 145 234 235 245 345

How can I return it as a list?

Thank you!

CodePudding user response:

You would want to let the functions pass variables each other, e.g., as lists, rather than printing inside the functions. The following is a minimally modified version of your code:

def k_size_subsets(n, k):
    list1 = [i for i in range(1, n   1)]
    data = [0] * k
    return help_fun(n, k, data, list1,0,0)

def help_fun(n, k, data, list1,index, i):
    if index == k:
        return [more_help(data)]
    if i >= n:
        return []
    data[index] = list1[i]
    return help_fun(n, k, data, list1, index  1, i 1)   help_fun(n, k, data, list1, index, i 1)

def more_help(lst):
    str1 = ''
    for i in range(len(lst)):
        str1  = str(lst[i])
    return str1

print(k_size_subsets(5, 3))
# ['123', '124', '125', '134', '135', '145', '234', '235', '245', '345']

CodePudding user response:

Here you just have a single print statement, but here are a couple of solutions of possible interest, but which could help to turn all the output into a list even if you have multiple print statements in different places, while still minimising the change to the overall structure of your code.

Option 1) Use a list in the module scope called _output, and in place of any print statement, append to that list. This avoids having to pass other variables around between your functions. Note the use of clear() to empty the list before starting.

_output = []

def k_size_subsets(n, k):
    _output.clear()
    list1 = [i for i in range(1, n   1)]
    data = [0] * k
    help_fun(n, k, data, list1,0,0)
    return _output

def help_fun(n, k, data, list1,index, i):
    if index == k:
        more_help(data)
        return
    if i >= n:
        return
    data[index] = list1[i]
    help_fun(n, k, data, list1, index  1, i 1)
    help_fun(n, k, data, list1, index, i 1)

def more_help(list):
    str1 = ''
    for i in range(len(list)):
        str1  = str(list[i])
    _output.append(str1)

print(k_size_subsets(5, 3))

Option 2) Turn your function into a generator. If in every place where you have a print statement, you turn it into yield, and you change the calls to use yield from, then you could do something like this. Note the list comprehension in the caller to actually iterate and build a list.

Warning: this solution looks very similar in terms of the syntax, but involves some more advanced concepts.

def k_size_subsets(n, k):
    list1 = [i for i in range(1, n   1)]
    data = [0] * k
    yield from help_fun(n, k, data, list1,0,0)

def help_fun(n, k, data, list1,index, i):
    if index == k:
        yield from more_help(data)
        return
    if i >= n:
        return
    data[index] = list1[i]
    yield from help_fun(n, k, data, list1, index  1, i 1)
    yield from help_fun(n, k, data, list1, index, i 1)

def more_help(lst):
    str1 = ''
    for i in range(len(lst)):
        str1  = str(lst[i])
    yield str1

print([x for x in k_size_subsets(5, 3)])
  • Related