-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimal-host.py
More file actions
21 lines (21 loc) · 875 Bytes
/
optimal-host.py
File metadata and controls
21 lines (21 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pandas as pd
import numpy as np
def pick_host(friends):
df = pd.DataFrame([[x['name'],
x['location'][0],
x['location'][1],
x['location'][2]] for x in friends],
columns=['name', 'x', 'y', 'z'])
names = list(df['name'])
min_dist = 999999999999
min_name = ''
for name in names:
ser = df.loc[df['name'] == name, ['x', 'y', 'z']].iloc[0, :].copy()
# print(ser)
# print(df.loc[~(df['name'] == name), ['x', 'y', 'z']])
# print(df.loc[~(df['name'] == name), ['x', 'y', 'z']].sub(ser, axis=1))
curr_dist = np.sqrt(np.power(df.loc[~(df['name'] == name), ['x', 'y', 'z']].sub(ser, axis=1), 2).sum(axis=1)).sum()
if curr_dist < min_dist:
min_dist = curr_dist
min_name = name
return min_name