I have been running a program for a long time that I want to support. The algorithm is recursive, so sometimes subtasks in longer operations can be longer than short whole runs. I tried to use it, but I just finished the list full of generators at different levels of the recursive structure of the list (the list also has a multi-level hierarchy, call recording depth). Finally, I made a simple option for printing the answers, but in the end it prints the answers. I should not print only the results of recursive calls, as well as the results needed for subsequent processing before printing.
Is there a simple pattern for calling a top-level function to get values, but recursive calls to return answers? Should I use for loops over the recursive results of the call or make the list () of third-party users recursive calls? Should I just enter the depth parameter and return with depth> 0 and get at depth 0?
In any case, is there an easy way to rotate a single response to a line outputting a call to return the lines to the main Python program? Or should I return the full list from the module call? I could easily run the version of the os call in a separate interpreter with 'bg' on a Linux system, could I?
The problem is the complete coverage problem, an example useful for my application, for example, would be to do the same as without these combinations, only adding numbers until they exceed the limit, recursively returning the exact amounts:
from __future__ import print_function
def subset(seq, mask):
""" binary mask of len(seq) bits, return generator for the sequence """
return (c for ind,c in enumerate(seq) if mask & (1<<ind))
numbers = [1, 5, 3, 9, 4]
print('Numbers: ',numbers)
print('Possible sums',min(numbers),'..',sum(numbers))
for i in range(1,2**len(numbers)):
sub = list(subset(numbers, i))
print(sum(sub),'=',' + '.join(str(s) for s in sub))
print()
target = 11
print ('Finding subsequence for sum = %i' % target)
check = None
for check in (subset(numbers, mask)
for mask in range(1,2**len(numbers))
if sum(subset(numbers, mask))==target):
print (' + '.join(str(s) for s in check), ' = ', target)
if not check:
print('No solutions')