forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_292.py
52 lines (43 loc) · 1.19 KB
/
problem_292.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
class Group:
def __init__(self):
self.members = set()
self.enemies = set()
def __repr__(self):
return str(self.members)
def add_student(self, student, enemies):
self.members.add(student)
self.enemies |= set(enemies)
def get_groups(enemy_map):
students = enemy_map.keys()
first, second = Group(), Group()
for student in students:
if not first.members:
first.add_student(student, enemy_map[student])
elif student not in first.enemies:
first.add_student(student, enemy_map[student])
elif not second.members:
second.add_student(student, enemy_map[student])
elif student not in second.enemies:
second.add_student(student, enemy_map[student])
if len(first.members) + len(second.members) == len(students):
return first.members, second.members
return False
# Tests
enemy_map = {
0: [3],
1: [2],
2: [1, 4],
3: [0, 4, 5],
4: [2, 3],
5: [3]
}
assert get_groups(enemy_map) == ({0, 1, 4, 5}, {2, 3})
enemy_map = {
0: [3],
1: [2],
2: [1, 3, 4],
3: [0, 2, 4, 5],
4: [2, 3],
5: [3]
}
assert get_groups(enemy_map) == False