-
Notifications
You must be signed in to change notification settings - Fork 12
/
programa.py
executable file
·60 lines (41 loc) · 1.85 KB
/
programa.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
#!/usr/bin/env python
import unittest
import math
import random
class Friend:
def __init__(self, id, location):
self.id = id
self.location = location
def __str__(self):
return 'id={} location={}'.format(self.id, self.location)
def get_closest_friends(self, friends):
result = [{'id': friend,
'distance': calc_distance(self.location, friend.location)}
for friend in friends if friend is not self]
result.sort(key=lambda x: x['distance'])
return set([e['id'] for e in result[:3]])
def calc_distance(a, b):
delta_x = a[0] - b[0]
delta_y = a[1] - b[1]
return math.sqrt(pow(delta_x, 2) + pow(delta_y, 2))
class ProgramaTestCase(unittest.TestCase):
def setUp(self):
self.friends = []
for i in range(random.randint(0, 20)):
self.friends.append(Friend(id=i+2, location=[i*2, i*3]))
def test_distance_from_two_points(self):
self.assertEqual(calc_distance([0, 0], [3, 4]), 5)
self.assertAlmostEqual(calc_distance([0, 0], [4, 5]), 6.4, 1)
self.assertNotAlmostEqual(calc_distance([0, 0], [4, 5]), 6.5, 1)
def test_closest_three_friends_hard_mode(self):
currentFriend = Friend(id=1, location=[3000, 9518570])
self.assertEqual(currentFriend.get_closest_friends(self.friends), set(self.friends[-3:]))
def test_closest_three_friends(self):
currentFriend = Friend(id=1, location=[0, 0])
self.assertEqual(currentFriend.get_closest_friends(self.friends), set(self.friends[:3]))
def test_closest_three_friends_with_current_friend(self):
currentFriend = Friend(id=1, location=[0, 0])
self.friends.append(currentFriend)
self.assertEqual(currentFriend.get_closest_friends(self.friends), set(self.friends[:3]))
if __name__ == '__main__':
unittest.main()