## Iterators and Generators: Difficult **Solutions**

### code writing

Write a generator function that goes through a tree and yields all of the paths in the tree where the leaf value in the path is equal to val, where each path is represented as a linked list.

def tree_gen(t, val):

>> t = Tree(1, [Tree(2, [Tree(3)]), Tree(7), Tree(3)])

>>> list(tree_gen(t, 3))

[Link(1, Link(2, Link(3))), Link(1, Link(3))]

if **t.is_leaf()**:

**if t.label == val:
yield Link(t.label)
else:
yield Link.empty**

for branch in t.branches:

**for path in tree_gen(branch)**

**yield Link(t.label, path)**

Write a generator function that yields the elements in a list in order from greatest to least. You can assume there is at least one element in the list, and that all elements are unique.

def sort_list_gen(lst):

>> lst = [8, 1, 5, 7]

>>> gen = sort_list_gen(lst)

>>> list(gen)

[8, 7, 5, 1]

**while len(lst) > 0:
yield max(lst)
lst.remove(max(lst))**