Is there a way to flip the response list from a script as values?

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')
+3
source share
2 answers

You don't know much about what you're actually trying to do, but here is my best guess (note: you will need Python 2.6):

def do_stuff(num):
    children = [ _do_stuff(x + 1) for x in range(num) ]
    for child in children:
        child.send(None)

    count = 0
    while children:
        child = children.pop(0)
        try:
            count += child.send(count)
        except StopIteration:
            continue
        children.append(child)

def _do_stuff(num):
    to_add = 0
    for x in range(num):
        from_parent = (yield (to_add + x))
        print "child %s got %s" %(num, from_parent)
        to_add += from_parent

Which will work as follows:

>>> do_stuff (3)
child 1 got 0
child 2 got 0
child 3 got 1
child 2 got 3
child 3 got 3
child 3 got 9

Sorry this example is a bit confusing - my brain is not suitable for a better example right now.

, : * , * ( O (n)) - . dequeue * child.send(None) "" (.. yield)

+1

. , .

; , . , , , :

def descendants(node):
   for child in children(node):
      yield child
      for descendant in descendants(child):
         yield descendant

node , . - children , node, descendants node .

, . ( ) , , . , ; , , ( ) . , ; :

for x in get_things(param):
   print x

get_things() ( ) , .

get_things() , , .

0

Source: https://habr.com/ru/post/1768779/


All Articles