From 1cec72cbf726176a142279bfa44dd54860887d67 Mon Sep 17 00:00:00 2001 From: Ifeanyi Aladi Date: Sat, 8 Apr 2023 06:39:22 +0100 Subject: [PATCH 1/2] Made the grid file easier to read and understand --- code/grid.py | 112 +++++++++++---------------------------------------- 1 file changed, 23 insertions(+), 89 deletions(-) diff --git a/code/grid.py b/code/grid.py index d716575..497ff4f 100644 --- a/code/grid.py +++ b/code/grid.py @@ -9,107 +9,41 @@ 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 print_row(): - print_beams() - do_four(print_posts) - -def print_grid(): - do_twice(print_row) - print_beams() - -print_grid() +def operators(): + add = "+ " + sub = "- " * 4 + print(add,sub,add,sub,add) -# 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('+', end=' ') - -def print_dash(): - print('-', end=' ') - -def print_bar(): - print('|', end=' ') -def print_space(): - print(' ', end=' ') +"""Create a function that prints the straight beams and adds the appropriate amount of space between them""" -def print_end(): - print() +def straight_beams(): + print("|" + " " * 11 + "|" + " " * 11 + "|") -def nothing(): - "do nothing" +"""Create a function that would repeat the straight_beams function 4 times when passed as an argument""" -def print1beam(): - one_four_one(nothing, print_dash, print_plus) +def do_four_times(func): + func() + func() + func() + func() -def print1post(): - one_four_one(nothing, print_space, print_bar) +"""The final function that arranges the previous functions in the order that creates the grid""" -def print4beams(): - one_four_one(print_plus, print1beam, print_end) +def grid_maker(): + operators() + do_four_times(straight_beams) + operators() + do_four_times(straight_beams) + operators() -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. - ---- Allen -""" -print(comment) +grid_maker() \ No newline at end of file From a3d39606c8e10a16c57ceff29c139d44d1a0fd93 Mon Sep 17 00:00:00 2001 From: Ifeanyi Aladi Date: Sat, 8 Apr 2023 06:55:52 +0100 Subject: [PATCH 2/2] Made two lines easier to understand I edited lines 30 and 70 and explained what happens in those lines with comments. --- code/grid.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/code/grid.py b/code/grid.py index 497ff4f..ca89de9 100644 --- a/code/grid.py +++ b/code/grid.py @@ -26,7 +26,8 @@ def operators(): """Create a function that prints the straight beams and adds the appropriate amount of space between them""" def straight_beams(): - print("|" + " " * 11 + "|" + " " * 11 + "|") + #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 + "|") """Create a function that would repeat the straight_beams function 4 times when passed as an argument""" @@ -46,4 +47,47 @@ def grid_maker(): operators() +grid_maker() + + + +"""For the 4x4 grid""" + +"""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 operators(): + add = "+ " + sub = "- " * 4 + + print(add,sub,add,sub,add,sub,add) + + +"""Create a function that prints the straight beams and adds the appropriate amount of space between them""" + +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 + "|") + +"""Create a function that would repeat the straight_beams function 4 times when passed as an argument""" + +def do_four_times(func): + func() + func() + func() + func() + +"""The final function that arranges the previous functions in the order that creates the grid""" + +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() + + grid_maker() \ No newline at end of file