1
Name:
Anonymous
2005-07-21 20:23
.
_..------.._ + ,'
.-' i `-. ,;
. .'. |. `. .'/
`-_ ;. , jl.____ `. .'.' I'M A FREAKING
`;-_ ;. ._..---""" """--.:' / TURTLE
. `.-..:,-" ,_.-'`'--'`'-9"h` .' .
`.`^.,-Ce/ .'"'. _.' /
`. '-h'_.._/0 " 0\/` {\
+ `. |'-^Y^- | // .
(`\ \_."._/\...-;..-.
`._'._,'` ``` _.:---''`
;-....----'''`
/ ( +
sk/itz | (`
`.^'
19
Name:
Anonymous
2011-03-03 1:33
WHAT IS THE BEST WAY TO DO THIS:
"""
Solution to Exercise X.X on page X of Think Python
Allen B. Downey
"""
# here is a mostly-straightforward solution to the
# two-by-two version of the grid.
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()
# here is a less-straightforward solution to the
# four-by-four grid
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()
comment = """
After writing a draft of the 4x4 grid, I noticed that many of the
functions had the same structure: they would do something, do
something else four times, and then do something else once.
So I wrote one_four_one, which takes three functions as arguments; it
calls the first one once, then uses do_four to call the second one
four times, then calls the third.
Then I rewrote print1beam, print1post, print4beams, print4posts,
print_row and print_grid using one_four_one.
Programming is an exploratory process. Writing a draft of a program
often gives you insight into the problem, which might lead you to
rewrite the code to reflect the structure of the solution.
20
Name:
Anonymous
2011-03-03 1:34
OR THIS:
print '+', 5*' - ', '+', 5*' - ', '+'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '+', 5*' ', '|'
print '+', 5*' - ', '+', 5*' - ', '+'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '|', 5*' ', '|', 5*' ', '|'
print '+', 5*' - ', '+', 5*' - ', '+'