Skip to content

Commit fefdbed

Browse files
committed
added solution to the grid printing homework.
1 parent e1ff021 commit fefdbed

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

‎Solutions/Session01/print_grid.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Chris' solution to the week 1 homework problem.
5+
6+
Note that we only did the basics of loops, and you can
7+
do all this without any loops at all, so that's what I did.
8+
9+
Note also that there is nore than one way to skin a cat -- or code a function
10+
"""
11+
12+
13+
def print_grid(size):
14+
"""
15+
print a 2x2 grid with a total size of size
16+
17+
:param size: total size of grid -- it will be rounded if not one
18+
more than a multiple of 2
19+
"""
20+
N = 2
21+
s = int( (size-1) // 2 ) # size of one grid box
22+
print "s:", s
23+
# top row
24+
top = ('+ ' + '- '*s)*N + '+' + '\n'
25+
middle = ('| ' + ' '*2*s)*N + '|' + '\n'
26+
27+
row = top + middle*s
28+
29+
grid = row*N + top
30+
31+
print grid
32+
33+
34+
def print_grid2(N, s):
35+
"""
36+
print a NxN grid with each box of size s
37+
38+
:param N: number of grid boxes (row and column)
39+
40+
:param s: size of each grid box
41+
"""
42+
# top row
43+
top = ('+ ' + '- '*s)*N + '+' + '\n'
44+
middle = ('| ' + ' '*2*s)*N + '|' + '\n'
45+
46+
row = top + middle*s
47+
48+
grid = row*N + top
49+
50+
print grid
51+
52+
53+
def print_grid3(size):
54+
"""
55+
same as print_grid, but calling print_grid2 to do the work
56+
"""
57+
N = 2
58+
s = (size-1) / 2 # size of one grid box
59+
print_grid2(N, s)
60+
61+
print_grid(11)
62+
print_grid(7)
63+
64+
print_grid2(3,3)
65+
print_grid2(3,5)
66+
67+
print_grid3(11)

0 commit comments

Comments
 (0)