How would you make this piece of code more simple/efficient? The problem should not be solved with one print statement, rather with functions
Could you also give me any advice how I can come to your simplified solution next time?
Code Used
def draw_pattern_1(p,m):
print(p, end=' ')
print(m, end=' ')
print(m, end=' ')
print(m, end=' ')
print(m, end=' ')
print(p, end=' ')
print(m, end=' ')
print(m, end=' ')
print(m, end=' ')
print(m, end=' ')
print(p)
def draw_pattern_2(l):
print(l, end=' ')
print(l, end=' ')
print(l)
def draw_grid(f_1,f_2,p,m,l):
draw_pattern_1(p,m)
draw_pattern_2(l)
draw_pattern_2(l)
draw_pattern_2(l)
draw_pattern_2(l)
draw_pattern_1(p,m)
draw_pattern_2(l)
draw_pattern_2(l)
draw_pattern_2(l)
draw_pattern_2(l)
draw_pattern_1(p,m)
p= "+"
m = "-"
l = "|"
draw_grid(draw_pattern_1,draw_pattern_2,p,m,l)
Output Generated
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
1 Answer 1
A potentially problematic part of the code above is that it does not generalize well: in essence, what your code achieves is decomposing the task into drawing lines with corners of the grid in them and drawing lines without those. The code also heavily uses lines like print(p, end=' ')
: at least in the case of a dash marker, this is fully equivalent to substituting that marker for a dash-space, and that is further equivalent to using print('- '*4, end='')
or, indeed, hard-coding the pattern. At the end of the day, this is not much different from using a couple of print statements in the first place:
row_of_cells = '+ - - - - + - - - - +\n' + '| | |\n'*4
print(row_of_cells * 2, end='')
print('+ - - - - + - - - - +')
This uses string multiplication to repeat the same pattern multiple times - this is good because the height of the cell and the number of rows become explicit and we do not have to print the cell character-by-character. Still, it looks bulky and, frankly, quite ugly. Another notable issue is that while it is somewhat easy to change how the grid is drawn, the code is heavily reliant on draw_pattern_1 and draw_pattern_2 outputting matching patterns. The benefit in printing the grid with functions as opposed to a fixed, hard-coded string lies elsewhere.
So, how do we get rid of lots and lots and repetitions (DRY: Don't Repeat Yourself!) and the lack of reusability (what if we want to draw a grid of a different size or shape)? Currently, your code allows us to change the symbols used and not much else: to draw a different grid, you would need to define draw_pattern_3 and draw_pattern_4, and then draw_pattern_5... You get the idea. This is only good if you get paid by SLOC.
One approach to handle it somewhat more elegantly would be figuring out algorithmically if the current character lands on the cell border or a corner and outputting a space otherwise:
corner = '+ '
vertical = '| '
horizontal = '- '
def grid(rows, cols, cell_h, cell_w):
for i in range(rows * cell_h + 1):
for j in range(cols * cell_w + 1):
if i % cell_h == 0 and j % cell_w == 0:
c = corner
elif i % cell_h == 0:
c = horizontal
elif j % cell_w == 0:
c = vertical
else:
c = ' '
print(c, end='')
print('') # newline
```
-
2\$\begingroup\$ This is an alternate solution. It is not a review of the code. As described in How do I write a good answer, "Every answer must make at least one insightful observation about the code in the question. Answers that merely provide an alternate solution with no explanation or justification do not constitute valid Code Review answers and may be deleted." \$\endgroup\$AJNeufeld– AJNeufeld2022年03月04日 17:37:43 +00:00Commented Mar 4, 2022 at 17:37
-
\$\begingroup\$ @AJNeufeld Thank you. I will edit the answer shortly. \$\endgroup\$Lodinn– Lodinn2022年03月04日 17:39:05 +00:00Commented Mar 4, 2022 at 17:39
-
\$\begingroup\$ Thanks a lot for your review and your advice! This was a big help \$\endgroup\$coder_sw99– coder_sw992022年03月04日 22:25:35 +00:00Commented Mar 4, 2022 at 22:25
Explore related questions
See similar questions with these tags.
draw_grid("unused", NotImplemented, p, m, l)
does the same thing asdraw_grid(draw_pattern_1, draw_pattern_2, p, m, l)
, which is probably not what you intended. \$\endgroup\$