Guest User

Untitled

a guest
Mar 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. 1 2 3
  2. 4 5 6
  3. 7 8 9
  4.  
  5. [i-1][j]
  6. [i][j-1]
  7. [i-1][j-1]
  8. [i+1][j]
  9. [i][j+1]
  10. [i+1][j+1]
  11. [i+1][j-1]
  12. [i-1][j+1]
  13.  
  14. # Size of "board"
  15. X = 10
  16. Y = 10
  17.  
  18. neighbors = lambda x, y : [(x2, y2) for x2 in range(x-1, x+2)
  19. for y2 in range(y-1, y+2)
  20. if (-1 < x <= X and
  21. -1 < y <= Y and
  22. (x != x2 or y != y2) and
  23. (0 <= x2 <= X) and
  24. (0 <= y2 <= Y))]
  25.  
  26. >>> print(neighbors(5, 5))
  27. [(4, 4), (4, 5), (4, 6), (5, 4), (5, 6), (6, 4), (6, 5), (6, 6)]
  28.  
  29. from itertools import product, starmap
  30.  
  31. x, y = (8, 13)
  32. cells = starmap(lambda a,b: (x+a, y+b), product((0,-1,+1), (0,-1,+1)))
  33.  
  34. // [(8, 12), (8, 14), (7, 13), (7, 12), (7, 14), (9, 13), (9, 12), (9, 14)]
  35. print(list(cells)[1:])
  36.  
  37. from itertools import product
  38.  
  39. size = 3
  40.  
  41. def neighbours(cell):
  42. for c in product(*(range(n-1, n+2) for n in cell)):
  43. if c != cell and all(0 <= n < size for n in c):
  44. yield c
  45.  
  46. >>> list(neighbours((2,2)))
  47. [(1, 1), (1, 2), (2, 1)]
  48.  
  49. for x_ in range(max(0,x-1),min(width,x+2)):
  50. for y_ in range(max(0,y-1),min(height,y+2)):
  51. if (x,y)==(x_,y_): continue
  52. # do stuff with the neighbours
  53.  
  54. >>> a=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  55. >>> width=height=3
  56. >>> x,y=0,2
  57. >>> for x_ in range(max(0,x-1),min(width,x+2)):
  58. ... for y_ in range(max(0,y-1),min(height,y+2)):
  59. ... if (x,y)==(x_,y_): continue
  60. ... print a[x_][y_]
  61. ...
  62. 2
  63. 5
  64. 6
  65.  
  66. def top(matrix, x, y):
  67. try:
  68. return matrix[x][y - 1];
  69. except IndexError:
  70. return None
  71.  
  72. (x - 1, y - 1) (x, y - 1) (x + 1, y - 1)
  73. (x - 1, y) (x, y) (x + 1, y)
  74. (x - 1, y + 1) (x, y + 1) (x + 1, y + 1)
  75.  
  76. >>> import itertools
  77. >>> def sl(lst, i, j):
  78. il, iu = max(0, i-1), min(len(lst)-1, i+1)
  79. jl, ju = max(0, j-1), min(len(lst[0])-1, j+1)
  80. return (il, iu), (jl, ju)
  81.  
  82. >>> lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  83. >>> tup = 0, 2
  84. >>> [lst[i][j] for i, j in itertools.product(*sl(lst, *tup)) if (i, j) != tup]
  85. [2, 5, 6]
  86.  
  87. def neighboring( array ):
  88. nn,mm = len(array), len(array[0])
  89. offset = (0,-1,1) # 0 first so the current cell is the first in the gen
  90. indices = ( (i,j) for i in range(nn) for j in range(mm) )
  91. for i,j in indices:
  92. all_neigh = ( (i+x,j+y) for x in offset for y in offset )
  93. valid = ( (i,j) for i,j in all_neigh if (0<=i<nn) and (0<=j<mm) ) # -1 is a valid index in normal lists, but not here so throw it out
  94. yield valid.next(), valid ## first is the current cell, next are the neightbors
  95.  
  96. for (x,y), neigh in neighboring( l ):
  97. print l[x][y], [l[x][y] for x,y in neigh]
  98.  
  99. startingRow = x / n * n;
  100. startingCol = y/ n * n
  101.  
  102. neighbors = [(x+a[0], y+a[1]) for a in
  103. [(-1,0), (1,0), (0,-1), (0,1)]
  104. if ( (0 <= x+a[0] < w) and (0 <= y+a[1] < h))]
Add Comment
Please, Sign In to add comment