forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_grid.py
More file actions
executable file
·93 lines (67 loc) · 2.03 KB
/
Copy pathprint_grid.py
File metadata and controls
executable file
·93 lines (67 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
"""
Chris' solution to the grid printing Exercise.
Note that we only did the basics of loops, and you can
do all this without any loops at all, so that's what I did.
Note also that there is more than one way to skin a cat --
or code a function
"""
def print_grid_trivial():
"""
Did anyone come up with the most trivial possible solution?
Note that this may the the wy to go, oif this all you need to do:
print something specific once. Why write fancy code for that?
"""
print("""
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
""")
def print_grid(size):
"""
print a 2x2 grid with a total size of size
:param size: total size of grid -- it will be rounded if not one more than
a multiple of 2
"""
number = 2
box_size = int((size - 1) // 2) # size of one grid box: integer division
print("box_size:", box_size)
# top row
top = ('+ ' + '- ' * box_size) * number + '+' + '\n'
middle = ('| ' + ' ' * 2 * box_size) * number + '|' + '\n'
row = top + middle * box_size
grid = row * number + top
print(grid)
def print_grid2(number, size):
"""
print a number x number grid with each box of size width and height
:param number: number of grid boxes (row and column)
:param size: size of each grid box
"""
# top row
top = ('+ ' + '- ' * size) * number + '+' + '\n'
middle = ('| ' + ' ' * 2 * size) * number + '|' + '\n'
row = top + middle * size
grid = row * number + top
print(grid)
def print_grid3(size):
"""
same as print_grid, but calling print_grid2 to do the work
"""
number = 2
box_size = (size - 1) // 2 # size of one grid box: note integer divsion!
print_grid2(number, box_size)
print_grid_trivial()
print_grid(11)
print_grid(7)
print_grid2(3, 3)
print_grid2(3, 5)
print_grid3(11)