Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import seed, randrange
- import sys
- dim = 10
- def display_grid():
- for i in range(dim):
- print(' ', end = '')
- for j in range(dim):
- print(' 1', end = '') if grid[i][j] else print(' 0', end = '')
- print()
- print()
- def size_of_largest_parallelogram():
- #transformation of the grid to 0 and 1
- for i in range(dim):
- for j in range(dim):
- if grid[i][j] != 0:
- grid[i][j] = 1
- else:
- grid[i][j] = 0
- count_current = 0
- count_max = 0
- L = []
- #loop over each grid
- for a in range(7,8):
- for b in range(7,8):
- #c:column slice; r:row slice
- for r in range(b,dim):
- for c in range(a,dim):
- # slice one more grid to the right
- for e in range(c+2, dim + 1):
- for f in range(r+2, dim +1):
- G = [grid[i][c:e]for i in range(r,f)]
- #print out G
- for i in range(f-r):
- for j in range(e-c):
- print(G[i][j],end = ' ')
- L.append(G[i][j])
- #new line for the new row of the grid
- print()
- #iterate over columns
- print()
- #check if 0 in the parrallogram,if yes, parrallogram invalid otherwise record count_current
- if 0 in L:
- L = []
- else:
- count_current = len(L)
- if count_current > count_max:
- count_max = count_current
- L = []
- print(count_max)
- # Possibly add code for other functions
- try:
- for_seed, n = [int(i) for i in
- input('Enter two integers, the second one being strictly positive: ').split()]
- if n <= 0:
- raise ValueError
- except ValueError:
- print('Incorrect input, giving up.')
- sys.exit()
- seed(for_seed)
- grid = [[randrange(n) for _ in range(dim)] for _ in range(dim)]
- print('Here is the grid that has been generated:')
- display_grid()
- size = size_of_largest_parallelogram()
- if size:
- print('The largest parallelogram with horizontal sides has a size of', size, end = '.\n')
- else:
- print('There is no parallelogram with horizontal sides.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement