Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. def calculate_mine_count(minefield: list) -> list:
  2. """
  3. For each cell in minefield, calculate how many mines are nearby.
  4.  
  5. This function cannot modify the original list.
  6. So, the result should be a new list (or copy of original).
  7.  
  8. ....
  9. ..x.
  10. X.X.
  11. x..X
  12.  
  13. =>
  14.  
  15. 0111
  16. 13x2
  17. X3X3
  18. x32X
  19.  
  20. :param minefield:
  21. :return:
  22. """
  23. new_minefield = deepcopy(minefield)
  24. x = "x" or "X"
  25. for index, element in enumerate(new_minefield):
  26. for second_index, i in enumerate(element):
  27. count = 0
  28. if i != element[0]:
  29. if new_minefield[index][second_index - 1] == x:
  30. count += 1
  31. if i != element[-1]:
  32. if new_minefield[index][second_index + 1] == x:
  33. count += 1
  34. if element != new_minefield[0] and i != element[0]:
  35. if new_minefield[index - 1][second_index - 1] == x:
  36. count += 1
  37. if element != new_minefield[0]:
  38. if new_minefield[index - 1][second_index] == x:
  39. count += 1
  40. if element != new_minefield[0] and i != element[-1]:
  41. if new_minefield[index - 1][second_index + 1] == x:
  42. count += 1
  43. if element != new_minefield[-1] and i != element[0]:
  44. if new_minefield[index + 1][second_index - 1] == x:
  45. count += 1
  46. if element != new_minefield[-1]:
  47. if new_minefield[index + 1][second_index] == x:
  48. count += 1
  49. if element != new_minefield[-1] and i != element[-1]:
  50. if new_minefield[index + 1][second_index + 1] == x:
  51. count += 1
  52. new_minefield[index][second_index] = str(count)
  53. if i == x:
  54. new_minefield[index][second_index] = i
  55. return new_minefield
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement