def multiplication_table(rows, cols):
"""A ROWS x COLS multiplication table wwhere row x column y
(element [x][y]) contains xy. Example:
>>> multiplication_table(4, 3)
[[0, 0, 0], [0, 1, 2], [0, 2, 4], [0, 3, 6]]
"""
def triangle(rows):
"""A ROWSxROWS lower-triangular array
containing "*"s."""
def numbered_triangle(rows):
"""A ROWSxROWS lower-triangular array whose elements
are integers, starting at 0 going left-to-right,
up-to-down.
>>> numbered_triangle(3)
[ [ 0 ], [ 1, 2 ], [ 3, 4, 5 ] ]"""
def make_tree(label, kids = [])
"""A (sub)tree with given LABEL at its root, whose children
are KIDS."""
return [ label ] + kids
def label(tree):
"""The label on TREE."""
return tree[0]
def children(tree):
"""The immediate descendants of TREE (each a tree)."""
return tree[1:]
def isleaf(tree):
"""True if TREE is a leaf node."""
return len(tree) == 1
def count_leaves(tree):
"""The number of leaf nodes in TREE."""
def leaf_labels(tree):
"""A list of the labels of all leaves in TREE."""
def eval(expr):
"""The value yielded by the computation represented by
expression tree EXPR. Assumes all leaves are numbers
and all inner-node labels are operators."""