Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt
- def createZeroMatrix(n,m):
- output = []
- for i in range(n):
- row = []
- for j in range(m):
- row.append("N")
- output.append(row)
- return output
- def mTightPrint(m):
- for i in range(len(m)):
- line = ''
- for j in range(len(m[0])):
- line += str(m[i][j])
- print(line)
- def nearestPizza(x, y, sites):
- min_index = 0
- min_dist = distance(x, y, sites[0][0], sites[0][1])
- shared = False
- for i in range(1, len(sites)):
- site = sites[i]
- d = distance(x, y, site[0], site[1])
- if d < min_dist:
- min_dist = d
- min_index = i
- shared = False
- elif d == min_dist:
- shared = True
- if shared:
- return "X"
- else:
- return min_index
- def distance(x1, y1, x2, y2):
- x = x2 - x1
- y = y2 - y1
- return sqrt(x**2 + y**2)
- def PDMap(r,c,sites):
- map = createZeroMatrix(r,c)
- for i in range(r):
- for j in range(c):
- map[i][j] = nearestPizza(i, j, sites)
- #mTightPrint(map)
- return map
- solution = PDMap(10,10,[[2,3],[4,9],[7,2]])
- expected = [[0, 0, 0, 0, 0, 0, 0, 'X', 1, 1],
- [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
- [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
- [0, 0, 0, 0, 0, 0, 'X', 1, 1, 1],
- ['X', 0, 0, 0, 0, 0, 1, 1, 1, 1],
- [2, 2, 2, 2, 2, 'X', 1, 1, 1, 1],
- [2, 2, 2, 2, 2, 2, 1, 1, 1, 1],
- [2, 2, 2, 2, 2, 2, 2, 1, 1, 1],
- [2, 2, 2, 2, 2, 2, 2, 1, 1, 1],
- [2, 2, 2, 2, 2, 2, 2, 'X', 1, 1]]
- print("Our solution:")
- mTightPrint(solution)
- print("\nExpected solution:")
- mTightPrint(expected)
- print("\nMatch:", solution == expected)
- """output:
- Our solution:
- 0000000X11
- 0000000111
- 0000000111
- 000000X111
- X000001111
- 22222X1111
- 2222221111
- 2222222111
- 2222222111
- 2222222X11
- Expected solution:
- 0000000X11
- 0000000111
- 0000000111
- 000000X111
- X000001111
- 22222X1111
- 2222221111
- 2222222111
- 2222222111
- 2222222X11
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement