In [49]:
def f(x):
return x + 1
def g(x):
return 2 * f(x)
def h(x):
return f(2 * x)
In [23]:
def right_justify(string):
print ' ' * (70 - len(string)) + string
In [25]:
right_justify('hello')
right_justify('hi')
In [27]:
def do_twice(f, arg):
f(arg)
f(arg)
def print_twice(arg):
print arg
print arg
print 'do_twice output'
do_twice(print_twice, 'spam')
def do_four(f, arg):
do_twice(f, arg)
do_twice(f, arg)
print 'do_four output'
do_four(print_twice, 'spam')
In [29]:
def do_twice(f):
f()
f()
def do_four(f):
do_twice(f)
do_twice(f)
def print_beam():
print '+ - - - -',
def print_post():
print '| ',
def print_beams():
do_twice(print_beam)
print '+'
def print_posts():
do_twice(print_post)
print '|'
def print_row():
print_beams()
do_four(print_posts)
def print_grid():
do_twice(print_row)
print_beams()
print_grid()
In [45]:
def one_four_one(f, g, h):
f()
do_four(g)
h()
def print_plus():
print '+',
def print_dash():
print '-',
def print_bar():
print '|',
def print_space():
print ' ',
def print_end():
print
def nothing():
"do nothing"
def print1beam():
one_four_one(nothing, print_dash, print_plus)
def print1post():
one_four_one(nothing, print_space, print_bar)
def print4beams():
one_four_one(print_plus, print1beam, print_end)
def print4posts():
one_four_one(print_bar, print1post, print_end)
def print_row():
one_four_one(nothing, print4posts, print4beams)
def print_grid():
one_four_one(print4beams, print_row, nothing)
print_grid()
In [30]:
def get_line(symbol1, symbol2, n):
return (symbol1 + ' ' + (symbol2 + ' ')* 4) * n + symbol1 + '\n'
def get_row(n):
s = get_line('+','-', n)
s += get_line('|', ' ', n) * 4
return s
def print_grid(n):
g = get_row(n) * n
g += get_line('+','-', n)
print g
In [53]:
grid(12)