回答編集履歴
2
もっとはやい
answer
CHANGED
@@ -20,18 +20,19 @@
|
|
20
20
|
```
|
21
21
|
|
22
22
|
# 追記
|
23
|
-
複素数使ったりしながら色々試してたら半分の時間で完了するようになった。。。
|
23
|
+
複素数使ったりしながら色々試してたら半分以下の時間で完了するようになった。。。
|
24
24
|
|
25
25
|
```python
|
26
26
|
def filterclose(coordinates, distance):
|
27
27
|
iterable = iter(coordinates)
|
28
|
+
threshold = float(distance)
|
28
29
|
a = next(iterable)
|
29
30
|
a_comp = complex(*a)
|
30
31
|
yield a
|
31
32
|
for b in iterable:
|
32
33
|
b_comp = complex(*b)
|
33
34
|
diff = b_comp - a_comp
|
34
|
-
if abs(diff.real) >=
|
35
|
+
if abs(diff.real) >= threshold or abs(diff.imag) >= threshold:
|
35
36
|
yield b
|
36
37
|
a, a_comp = b, b_comp
|
37
38
|
```
|
1
より速いコード
answer
CHANGED
@@ -17,4 +17,21 @@
|
|
17
17
|
b_list = list(filterclose(a_list, 40))
|
18
18
|
|
19
19
|
print(b_list) # -> [[802, 1593], [93, 1019], [614, 1019], [1138, 1608], [1128, 1253]]
|
20
|
+
```
|
21
|
+
|
22
|
+
# 追記
|
23
|
+
複素数使ったりしながら色々試してたら半分の時間で完了するようになった。。。
|
24
|
+
|
25
|
+
```python
|
26
|
+
def filterclose(coordinates, distance):
|
27
|
+
iterable = iter(coordinates)
|
28
|
+
a = next(iterable)
|
29
|
+
a_comp = complex(*a)
|
30
|
+
yield a
|
31
|
+
for b in iterable:
|
32
|
+
b_comp = complex(*b)
|
33
|
+
diff = b_comp - a_comp
|
34
|
+
if abs(diff.real) >= distance or abs(diff.imag) >= distance:
|
35
|
+
yield b
|
36
|
+
a, a_comp = b, b_comp
|
20
37
|
```
|