class Tree: """ >>> t = Tree(3, [Tree(2, [Tree(5)]), Tree(4)]) >>> t.label 3 >>> t.branches[0].label 2 >>> t.branches[1].is_leaf() True """ def __init__(self, label, branches=[]): for b in branches: assert isinstance(b, Tree) self.label = label self.branches = list(branches) def is_leaf(self): return not self.branches def __repr__(self): if self.branches: branch_str = ', ' + repr(self.branches) else: branch_str = '' return 'Tree({0}{1})'.format(self.label, branch_str) def __str__(self): def print_tree(t, indent=0): tree_str = ' ' * indent + str(t.label) + "\n" for b in t.branches: tree_str += print_tree(b, indent + 1) return tree_str return print_tree(self).rstrip() def add_d_leaves(t, v): """Add d leaves containing v to each node at every depth d. >>> t_one_to_four = Tree(1, [Tree(2), Tree(3, [Tree(4)])]) >>> print(t_one_to_four) 1 2 3 4 >>> add_d_leaves(t_one_to_four, 5) >>> print(t_one_to_four) 1 2 5 3 4 5 5 5 >>> t1 = Tree(1, [Tree(3)]) >>> add_d_leaves(t1, 4) >>> t1 Tree(1, [Tree(3, [Tree(4)])]) >>> t2 = Tree(2, [Tree(5), Tree(6)]) >>> t3 = Tree(3, [t1, Tree(0), t2]) >>> print(t3) 3 1 3 4 0 2 5 6 >>> add_d_leaves(t3, 10) >>> print(t3) 3 1 3 4 10 10 10 10 10 10 0 10 2 5 10 10 6 10 10 10 """ "*** YOUR CODE HERE ***"
Function that: class VendingMachine(object): """A vending machine that vends some product for some price. >>> v = VendingMachine('candy', 10) >>> v.vend() 'Machine is out of stock.' >>> v.deposit(15) 'Machine is out of stock. Here is your $15.' >>> v.restock(2) 'Current candy stock: 2' >>> v.vend() 'You must deposit $10 more.' >>> v.deposit(7) 'Current balance: $7' >>> v.vend() 'You must deposit $3 more.' >>> v.deposit(5) 'Current balance: $12' >>> v.vend() 'Here is your candy and $2 change.' >>> v.deposit(10) 'Current balance: $10' >>> v.vend() 'Here is your candy.' >>> v.deposit(15) 'Machine is out of stock.