-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
54 lines (47 loc) · 1.09 KB
/
helpers.py
File metadata and controls
54 lines (47 loc) · 1.09 KB
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
from math import sqrt
def triangularize(collection):
"""
>>> triangularize([1, 2, 3, 4, 5, 6, 7])
[[1, 2, 3], [4, 5, 6], [7]]
"""
result = []
i = 0
for item in collection:
if i % 3 == 0:
result.append([item])
else:
result[-1].append(item)
i += 1
return result
def length(x, y):
"""
>>> length((0.0, 0.0), (1.0, 0.0))
1.0
>>> length((0.0, 1.0), (0.0, 0.0))
1.0
>>> length((0.0, 1.0), (2.0, 1.0))
2.0
>>> length((0.0, 0.0), (3.0, 4.0))
5.0
"""
return sqrt((x[0] - y[0])**2 + (x[1] - y[1])**2)
def length_from(points, start):
"""
>>> length_from([(1.0,1.0), (2.0,1.0), (3.0,1.0)], (0.0,1.0))
6.0
"""
result = .0
old = start
for point in points:
result += length(old, point)
old = point
result += length(old, start)
return result
def cost(solution, start):
result = .0
for triangle in solution:
result += length_from(triangle, start)
return result
if __name__ == "__main__":
import doctest
doctest.testmod()