Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. from random import seed, randrange
  2. import sys
  3.  
  4.  
  5. dim = 10
  6.  
  7.  
  8. def display_grid():
  9.     for i in range(dim):
  10.         print('    ', end = '')
  11.         for j in range(dim):
  12.             print(' 1', end = '') if grid[i][j] else print(' 0', end = '')
  13.         print()
  14.     print()
  15.  
  16.  
  17. def size_of_largest_parallelogram():
  18.     #transformation of the grid to 0 and 1
  19.     for i in range(dim):
  20.         for j in range(dim):
  21.             if grid[i][j] != 0:
  22.                 grid[i][j] = 1
  23.             else:
  24.                 grid[i][j] = 0
  25.  
  26.    
  27.     count_current = 0
  28.     count_max = 0
  29.     L = []
  30.     #loop over each grid
  31.     for a in range(7,8):
  32.         for b in range(7,8):
  33.             #c:column slice; r:row slice
  34.             for r in range(b,dim):
  35.                 for c in range(a,dim):
  36.                     # slice one more grid to the right    
  37.                     for e in range(c+2, dim + 1):
  38.                         for f in range(r+2, dim +1):
  39.                             G = [grid[i][c:e]for i in range(r,f)]
  40.                             #print out G
  41.                             for i in range(f-r):
  42.                                 for j in range(e-c):
  43.                                     print(G[i][j],end = ' ')
  44.                                     L.append(G[i][j])
  45.                                 #new line for the new row of the grid
  46.                                 print()
  47.                             #iterate over columns
  48.                             print()
  49.                             #check if 0 in the parrallogram,if yes, parrallogram invalid otherwise record count_current
  50.                             if 0 in L:
  51.                                 L = []
  52.                             else:
  53.                                 count_current = len(L)
  54.                                 if count_current > count_max:
  55.                                     count_max = count_current
  56.                                 L = []
  57.     print(count_max)
  58. # Possibly add code for other functions
  59.  
  60.  
  61. try:
  62.     for_seed, n = [int(i) for i in
  63.                            input('Enter two integers, the second one being strictly positive: ').split()]
  64.     if n <= 0:
  65.         raise ValueError
  66. except ValueError:
  67.     print('Incorrect input, giving up.')
  68.     sys.exit()
  69.  
  70. seed(for_seed)
  71. grid = [[randrange(n) for _ in range(dim)] for _ in range(dim)]
  72. print('Here is the grid that has been generated:')
  73. display_grid()
  74. size = size_of_largest_parallelogram()
  75. if size:
  76.     print('The largest parallelogram with horizontal sides has a size of', size, end = '.\n')
  77. else:
  78.     print('There is no parallelogram with horizontal sides.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement