. , . ( , ), , , .
, - , :
from collections import defaultdict
def expand(line, links, known):
print 'expand'
known.append(line)
for child in links[line[-1]]:
newline = line + child
yield expand(newline, links, known)
def trampoline(generator):
stack = [generator]
while stack:
try:
generator = stack.pop()
print 'next'
child = next(generator)
stack.append(generator)
stack.append(child)
except StopIteration:
pass
def main(pairs):
have_successors = set()
links = defaultdict(lambda: set())
for (start, end) in pairs:
have_successors.add(start)
links[end].add(start)
known = []
for node in set(links.keys()):
if node not in have_successors:
trampoline(expand(node, links, known))
for line in known:
print line
if __name__ == '__main__':
main([("L", "G"), ("A", "B"), ("B", "I"), ("B", "H"), ("B", "C"), ("F", "G"), ("D", "E"), ("D", "J"), ("E", "L"), ("C", "D"), ("E", "F"), ("J", "K")])
python2.7 - next(foo) foo.__next__() .
-, -, (), . , "" . , ...
-, "", defaultdict, / "". , defaultdict - .
, , , :
, , . " ", . ( , ), ( ?). , , . , , "" . , " ", , " , ...", "" .
, . - . . ? . , , ? "startIDs" ? " ". , .
. startIDs, ? , , . , , ( !: o). : -, , , - . , , , , .
, . i index count. ? - , . . if i < count - 1: : " "? , if branch == branches [-1]:, , .
. i . , for each alignment in alignments. , , : for each alignment in list(alignments).
, . buildAlignment . ? ? , , , . , "", . .
, - . , , , , . , . , , , , ...
[i , newline print .]
-, ? , ? , , ? my expand buildAlignment ().
( ), :
: python2.7 recurse.py
next
expand
next
expand
next
expand
next
expand
next
expand
next
expand
next
expand
next
next
...
, . "" - yield - python .
- . ( , expand), " ", . next(), , yield.
trampoline . next(). "" . next , yield, . trampoline() , next(), ... .. ..
"", StopIteration. , , , trampoline(). "" ( stack) next(). , ( yield), , , while, yield ( StopIteration).
, yield ! , . . , ! , ""! ( stack) - , .
, , , . , . google " ". , . , , , .
, , . , , , , , . ? A- > B, B- > C, C- > A,....