-
Notifications
You must be signed in to change notification settings - Fork 0
/
dojo.py
144 lines (111 loc) · 3.75 KB
/
dojo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def dist(p0, p1):
return ((p0[0] - p1[0]) ** 2 +
(p0[1] - p1[1]) ** 2)
def find_friends(coords):
if len(coords) == 1:
return [[]]
elif len(coords) == 2:
return [[1], [0]]
elif len(coords) == 4:
d01 = dist(coords[0], coords[1])
d02 = dist(coords[0], coords[2])
d03 = dist(coords[0], coords[3])
d12 = dist(coords[1], coords[2])
d13 = dist(coords[1], coords[3])
d23 = dist(coords[2], coords[3])
ans = []
d0s = sorted([(d01, 1), (d02, 2), (d03, 3)])
ans.append([idx for dist, idx in d0s])
d1s = sorted([(d01, 0), (d12, 2), (d13, 3)])
ans.append([idx for dist, idx in d1s])
d2s = sorted([(d02, 0), (d12, 1), (d23, 3)])
ans.append([idx for dist, idx in d2s])
d3s = sorted([(d03, 0), (d13, 1), (d23, 2)])
ans.append([idx for dist, idx in d3s])
return ans
if coords[2] == (10, 10):
return [[3, 1, 2], [3, 0, 2], [1, 3, 0], [0, 1, 2]]
return [[2, 1, 3], [2, 0, 3], [0, 1, 3], [1, 2, 0]]
else:
# longs, lats = zip(*coords)
# man_dist = list(map(sum, coords))
# man_dist = [a + b for a, b in coords]
d01 = dist(coords[0], coords[1])
d02 = dist(coords[0], coords[2])
d21 = dist(coords[2], coords[1])
ans = []
if d01 > d02:
ans.append([2, 1])
else:
ans.append([1, 2])
if d01 > d21:
ans.append([2, 0])
else:
ans.append([0, 2])
if d02 > d21:
ans.append([1, 0])
else:
ans.append([0, 1])
return ans
def test_1_tuple():
coords = [(0, 1)]
ans = find_friends(coords)
assert ans == [[]]
def test_2_tuples():
coords = [(0, 0), (0, 1)]
ans = find_friends(coords)
assert ans == [[1], [0]]
def test_2_tuples_2():
coords = [(1, 1), (1, 2)]
ans = find_friends(coords)
assert ans == [[1], [0]]
def test_3_tuples():
coords = [(0, 0), (0, 1), (0, 3)]
ans = find_friends(coords)
assert ans == [[1, 2], [0, 2], [1, 0]]
def test_3_tuples_2():
coords = [(0, 0), (0, 3), (0, 2)]
ans = find_friends(coords)
assert ans == [[2, 1], [2, 0], [1, 0]]
def test_3_tuples_3():
coords = [(0, 2), (0, 0), (0, 1)]
ans = find_friends(coords)
assert ans == [[2, 1], [2, 0], [0, 1]]
def test_3_tuples_4():
coords = [(0, 0), (0, 1), (0, 2)]
ans = find_friends(coords)
assert ans == [[1, 2], [0, 2], [1, 0]]
def test_3_tuples_5():
coords = [(0, 1), (0, 0), (0, 2)]
ans = find_friends(coords)
assert ans == [[1, 2], [0, 2], [0, 1]]
def test_3_tuples_x_difference():
coords = [(0, 0), (1, 0), (3, 0)]
ans = find_friends(coords)
assert ans == [[1, 2], [0, 2], [1, 0]]
def test_3_tuples_x_difference_2():
coords = [(0, 0), (3, 0), (1, 0)]
ans = find_friends(coords)
assert ans == [[2, 1], [2, 0], [0, 1]]
def test_3_tuples_not_manhattan():
coords = [(2, 0), (2, 2), (1, 1)]
ans = find_friends(coords)
assert ans == [[2, 1], [2, 0], [0, 1]]
def test_3_tuples_not_really_manhattan():
coords = [(1, -1), (1, 1), (0, 0)]
ans = find_friends(coords)
assert ans == [[2, 1], [2, 0], [0, 1]]
def test_4_tuples():
coords = [(1, -1), (1, 1), (0, 0), (10, 10)]
ans = find_friends(coords)
assert ans == [[2, 1, 3], [2, 0, 3], [0, 1, 3], [1, 2, 0]]
def test_4_tuples_2():
coords = [(1, -1), (1, 1), (10, 10), (0, 0)]
ans = find_friends(coords)
assert ans == [[3, 1, 2], [3, 0, 2], [1, 3, 0], [0, 1, 2]]
def test_4_tuples_hard():
coords = [(4, 0), (4, 2), (3, 2), (1, 7)]
ans = find_friends(coords)
assert ans == [[1, 2, 3], [2, 0, 3], [1, 0, 3], [2, 1, 0]]
# [t0, t1, t2, t3]
# [[ids...], [], [], []]