Advertisement
lukajda33

PDMap solution

Oct 19th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. from math import sqrt
  2.  
  3. def createZeroMatrix(n,m):
  4.     output = []
  5.     for i in range(n):
  6.         row = []
  7.         for j in range(m):
  8.             row.append("N")
  9.         output.append(row)
  10.     return output
  11.  
  12. def mTightPrint(m):
  13.     for i in range(len(m)):
  14.         line = ''
  15.         for j in range(len(m[0])):
  16.             line += str(m[i][j])
  17.         print(line)
  18.          
  19. def nearestPizza(x, y, sites):
  20.     min_index = 0
  21.     min_dist = distance(x, y, sites[0][0], sites[0][1])
  22.     shared = False
  23.     for i in range(1, len(sites)):
  24.         site = sites[i]
  25.         d = distance(x, y, site[0], site[1])
  26.         if d < min_dist:
  27.             min_dist = d
  28.             min_index = i
  29.             shared = False
  30.         elif d == min_dist:
  31.             shared = True
  32.     if shared:
  33.       return "X"
  34.     else:
  35.       return min_index
  36.    
  37. def distance(x1, y1, x2, y2):
  38.     x = x2 - x1
  39.     y = y2 - y1
  40.     return sqrt(x**2 + y**2)
  41.  
  42. def PDMap(r,c,sites):
  43.     map = createZeroMatrix(r,c)
  44.     for i in range(r):
  45.         for j in range(c):
  46.                 map[i][j] = nearestPizza(i, j, sites)
  47.     #mTightPrint(map)
  48.     return map
  49.  
  50. solution = PDMap(10,10,[[2,3],[4,9],[7,2]])
  51. expected = [[0, 0, 0, 0, 0, 0, 0, 'X', 1, 1],
  52. [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
  53. [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
  54. [0, 0, 0, 0, 0, 0, 'X', 1, 1, 1],
  55. ['X', 0, 0, 0, 0, 0, 1, 1, 1, 1],
  56. [2, 2, 2, 2, 2, 'X', 1, 1, 1, 1],
  57. [2, 2, 2, 2, 2, 2, 1, 1, 1, 1],
  58. [2, 2, 2, 2, 2, 2, 2, 1, 1, 1],
  59. [2, 2, 2, 2, 2, 2, 2, 1, 1, 1],
  60. [2, 2, 2, 2, 2, 2, 2, 'X', 1, 1]]
  61.  
  62. print("Our solution:")
  63. mTightPrint(solution)
  64. print("\nExpected solution:")
  65. mTightPrint(expected)
  66. print("\nMatch:", solution == expected)
  67.  
  68.  
  69. """output:
  70.  
  71. Our solution:
  72. 0000000X11
  73. 0000000111
  74. 0000000111
  75. 000000X111
  76. X000001111
  77. 22222X1111
  78. 2222221111
  79. 2222222111
  80. 2222222111
  81. 2222222X11
  82.  
  83. Expected solution:
  84. 0000000X11
  85. 0000000111
  86. 0000000111
  87. 000000X111
  88. X000001111
  89. 22222X1111
  90. 2222221111
  91. 2222222111
  92. 2222222111
  93. 2222222X11
  94. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement