-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOutbreaks.py
66 lines (49 loc) · 1.13 KB
/
Outbreaks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# GHC Codepath - Sandbox 8
# Module SE101
#!/bin/python3
import math
import os
import random
import re
import sys
import json
# Given a segment of a map with map coordinates as keys
# and count of outbreaks in the area as values
# - Find the center of the outbreak
# - treat each case as 1 data-point
# sample input:
# reported_outbreak = {
# "5,5": 10,
# "5,6": 8,
# "5,4": 8,
# "4,5": 8,
# "4,6": 8,
# "6,6": 7,
# "6,5": 8,
# "4,4": 8,
# "3,4": 4,
# "3,3": 2,
# "6,7": 2
# }
# sample output:
# The center is: "5,5"
def findCenter(points):
outbreak = json.loads(points)
sum_x = 0
sum_y = 0
sum_outbreak = 0
for key, value in outbreak.items():
x,y = map(int, key.split(','))
sum_x += x * value
sum_y += y * value
sum_outbreak += value
x_coordinate = str(round(sum_x/sum_outbreak))
y_coordinate = str(round(sum_y/sum_outbreak))
answer = ','.join([x_coordinate, y_coordinate])
return answer
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
points = input()
result = findCenter(points)
fptr.write(result + '\n')
fptr.close()