from Random Import SEED, randrange Import SYS Dim = 10 DEF display_grid (): for Row in Grid: Print ( ' ' , * Row) DEF display_grid1 (): for Row in Grid1: Print ( ' ' , * Row) # Grid1 is copy the results of the depth grid, where both are printed, to facilitate comparison result DEF one_shape (Row, COL, n_colour): IF Not (Row <len (Grid1 [0]) and Row> = 0 and COL> = 0 and COL < len (Grid1)): # must pay attention to the situation here is equal to 0 return # will and if not written on the back not benefit is if you do not meet the requirements, ie return, will not lead to value error IF gride1 [ Row] [COL] ==. 1 : Grid1 [Row] [COL] = n_colour # if eligible, modify the assignments one_shape (Row +. 1 , COL, n_colour) one_shape (Row -1 , COL, n_colour) one_shape (Row, COL +1 , n_colour) one_shape (Row, COL -1 , n_colour) # called recursively, that is constantly calling the current function to see if it meet the requirements of the else : return for_seed, Density = 0,8 seed(for_seed) grid = [[int(randrange(density) != 0) for _ in range(dim)] for _ in range(dim) ] grid1=[[i for i in row] for row in grid] print('Here is the grid that has been generated:') display_grid() print(' ') one_shape(0,0,2) display_grid1() print('The maximum number of spikes of some shape is:', max_number_of_spikes(nb_of_shapes) )