Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the grid file easier to read and understand #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 54 additions & 76 deletions code/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,107 +9,85 @@
License: http://creativecommons.org/licenses/by/4.0/
"""

from __future__ import print_function, division

# 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('+ - - - -', end=' ')
"""Create variables for the addition and subtraction operators
and print them in the order and number of times in which they appear on a line."""

def print_post():
print('| ', end=' ')

def print_beams():
do_twice(print_beam)
print('+')

def print_posts():
do_twice(print_post)
print('|')
def operators():
add = "+ "
sub = "- " * 4

print(add,sub,add,sub,add)

def print_row():
print_beams()
do_four(print_posts)

def print_grid():
do_twice(print_row)
print_beams()
"""Create a function that prints the straight beams and adds the appropriate amount of space between them"""

print_grid()

def straight_beams():
#You multiply the straight line-space convention by n-1 columns to get the 1st and middle columns and add the final column to complete the row"
print(("|" + " " * 11 ) * 2 + "|")

# here is a less-straightforward solution to the
# four-by-four grid
"""Create a function that would repeat the straight_beams function 4 times when passed as an argument"""

def one_four_one(f, g, h):
f()
do_four(g)
h()
def do_four_times(func):
func()
func()
func()
func()

def print_plus():
print('+', end=' ')
"""The final function that arranges the previous functions in the order that creates the grid"""

def print_dash():
print('-', end=' ')
def grid_maker():
operators()
do_four_times(straight_beams)
operators()
do_four_times(straight_beams)
operators()

def print_bar():
print('|', end=' ')

def print_space():
print(' ', end=' ')
grid_maker()

def print_end():
print()

def nothing():
"do nothing"

def print1beam():
one_four_one(nothing, print_dash, print_plus)
"""For the 4x4 grid"""

def print1post():
one_four_one(nothing, print_space, print_bar)
"""Create variables for the addition and subtraction operators
and print them in the order and number of times in which they appear on a line."""

def print4beams():
one_four_one(print_plus, print1beam, print_end)

def print4posts():
one_four_one(print_bar, print1post, print_end)
def operators():
add = "+ "
sub = "- " * 4
print(add,sub,add,sub,add,sub,add)

def print_row():
one_four_one(nothing, print4posts, print4beams)

def print_grid():
one_four_one(print4beams, print_row, nothing)
"""Create a function that prints the straight beams and adds the appropriate amount of space between them"""

print_grid()
def straight_beams():
#You multiply the straight line-space convention by n-1 columns to get the 1st and middle columns and add the final column to complete the row"
print(("|" + " " * 11 ) * 3 + "|")

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.
"""Create a function that would repeat the straight_beams function 4 times when passed as an argument"""

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.
def do_four_times(func):
func()
func()
func()
func()

Then I rewrote print1beam, print1post, print4beams, print4posts,
print_row and print_grid using one_four_one.
"""The final function that arranges the previous functions in the order that creates the grid"""

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.
print("Here's your 4x4 grid below")
def grid_maker():
operators()
do_four_times(straight_beams)
operators()
do_four_times(straight_beams)
operators()
do_four_times(straight_beams)
operators()

--- Allen
"""

print(comment)
grid_maker()