how to iteratively create a grid within a bigger grid in python

Solutions on MaxInterview for how to iteratively create a grid within a bigger grid in python by the best coders in the world

showing results for - "how to iteratively create a grid within a bigger grid in python"
Kayley
14 Sep 2017
1def grid_print(area, units):
2
3   print_Area = (area * area)
4
5   grid_rows = units + (units + 1) + 2
6
7   grid_cols = units + 2
8
9   if units % 2 == 0:           # If grid entry is even (it will end up making
10        grid_rows += 1               # the square uneven, so increase number of rows by 1
11                            # now grid is technically uneven
12        for i in range(print_Area):
13           for row in range(grid_rows):     # for each item in number of items(rows)
14               for col in range(grid_cols): # for each item in number of items(columns)
15                   if row == 0 or row == int(grid_rows/2) or row == grid_rows -1:    # if item is beginning, middle or end
16                   # --  Formatting beam structure  -- #
17                       if col == 0:                        # beginning, print '+' no '\n'
18                           print('+', end='')
19                       elif col == grid_cols -1:                # end, print '+'
20                       print('+')
21                       elif int(grid_cols/2) == col:            # middle:
22                           if grid_rows % 2 == 0:               # if grid is even, pad '+' with ' '
23                               print(' + ', end='')        # if grid is uneven, no padding
24                           else:                           # print '+' no '\n'
25                               print('+', end='')
26                       elif col % 2 == 0:                  # if col item is an even number
27                           print('-', end='')              # print '-' with no '\n'
28                       else:                               # else if col item is uneven item num
29                           print(' ', end='')              # print ' ' no '\n'
30                   else:
31                   # --  Formatting line structure  -- #
32                       if col == 0:                        # if column is at starting position 0
33                           print('|', end='')              # print '|' no '\n'
34                       elif col == int(grid_cols/2):            # if column is at middle pos
35                           if units % 2 == 0:               # print '|' no '\n'
36                               print(' | ', end='')        # (has padding if grid is even or not)
37                           else:
38                               print('|', end='')
39                       elif col == grid_cols -1:                # if column is at end position of grid
40                           print("|")                      # print '|'
41                       else:
42                           print(' ', end='')              # all other circumstances, print ' ' no '\n'