Advertisement
Guest User

PA4_Tim

a guest
Jan 29th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. """
  2. 1. search main_grid for first letter of first word to be searched
  3. 2. if match, check surrounding letters if second letter matches
  4. 3. if match again, continue checking if all letters in that direction match
  5. 4. if yes, output index of first letter. if no, search for next occurrence of letter and repeat from step 2
  6. 5. repeat for other words in words
  7. """
  8.  
  9.  
  10. R, C = map(int, input().split())
  11. print(R, C)
  12. main_grid = []
  13. results_a = []
  14. results_b = []
  15. for i in range(R):
  16. main_grid.append(input()) # grid to be searched
  17. N = int(input())
  18. words = []
  19. for i in range(N):
  20. words.append(input())
  21. print(words)
  22. print(words[0][2])
  23.  
  24. dirs = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]] # W, E, S, N, SW, NW, SE, NE
  25.  
  26. i = 0
  27. while i < N:
  28. for x in range(R):
  29. for y in range(C):
  30. z = 0
  31. if main_grid[x][y] == words[i][z]: # searches all coords matching first letter of words[i]
  32. while z < len(words[i]):
  33. coords = 0
  34. while coords < 7:
  35. a,b = dirs[coords]
  36. x += a
  37. y += b
  38. print(x)
  39. print(y)
  40. if main_grid[x][y] == words[i][z]:
  41. z += 1
  42. elif main_grid[x][y] == words[i][len(words[i])-1]:
  43. results_a.append(x)
  44. results_b.append(y)
  45. else:
  46. break
  47. coords += 1
  48.  
  49. print(results_a)
  50. print(results_b)
  51.  
  52. i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement