-
Notifications
You must be signed in to change notification settings - Fork 0
/
les_46_runnerTwo.py
78 lines (61 loc) · 2.26 KB
/
les_46_runnerTwo.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
import unittest
class Runner:
def __init__(self, name, speed=5):
self.name = name
self.distance = 0
self.speed = speed
def run(self):
self.distance += self.speed * 2
def walk(self):
self.distance += self.speed
def __str__(self):
return self.name
def __eq__(self, other):
if isinstance(other, str):
return self.name == other
elif isinstance(other, Runner):
return self.name == other.name
class Tournament:
def __init__(self, distance, *participants):
self.full_distance = distance
self.participants = list(participants)
def start(self):
finishers = {}
place = 1
while self.participants:
for participant in self.participants[:]:
participant.run()
if participant.distance >= self.full_distance:
finishers[place] = participant
place += 1
self.participants.remove(participant)
return finishers
class TournamentTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.all_results = {}
def setUp(self):
self.usain = Runner("Усэйн", speed=10)
self.andrey = Runner("Андрей", speed=9)
self.nick = Runner("Ник", speed=3)
@classmethod
def tearDownClass(cls):
for key, result in cls.all_results.items():
print(f"{key}: {result}")
def test_tournament_usain_nick(self):
tournament = Tournament(90, self.usain, self.nick)
results = tournament.start()
self.assertTrue(results[len(results)] == self.nick)
self.all_results["usain_nick"] = results
def test_tournament_andrey_nick(self):
tournament = Tournament(90, self.andrey, self.nick)
results = tournament.start()
self.assertTrue(results[len(results)] == self.nick)
self.all_results["andrey_nick"] = results
def test_tournament_usain_andrey_nick(self):
tournament = Tournament(90, self.usain, self.andrey, self.nick)
results = tournament.start()
self.assertTrue(results[len(results)] == self.nick)
self.all_results["usain_andrey_nick"] = results
if __name__ == "__main__":
unittest.main()