forked from glassus/terminale_nsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperstars_GL.py
149 lines (124 loc) · 3.63 KB
/
superstars_GL.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Find the problem statement by clicking "Instructions" on the right panel >>>
def findSuperstar(oracle):
"""
This method returns:
- a list of superstars if any
- an empty list otherwise
"""
print(f"The village counts {oracle.size} inhabitants.")
#print(f"You can ask the oracle whether #1 knows #3: {oracle.knows(1, 3)}.")
#
# Your code goes here
#
candidats = list(range(1, oracle.size + 1))
ignored_by = {}
know = {}
for k in candidats:
ignored_by[k] = []
know[k] = []
while len(candidats) > 1:
ind = len(candidats) // 2
for k in range(ind):
c1 = candidats[2*k]
c2 = candidats[2*k+1]
if oracle.knows(c1, c2):
candidats.remove(c1)
know[c2].append(c1)
else:
candidats.remove(c2)
ignored_by[c1].append(c2)
if candidats == []:
return []
candidat = candidats[0]
# test de validité 1 : le candidat ne connait personne
for perso in list(range(1, oracle.size + 1)):
if perso == candidat:
continue
if perso in ignored_by[candidat]:
continue
if oracle.knows(candidat, perso):
return []
# test de validité 2 : tout le monde connait le candidat
for perso in list(range(1, oracle.size + 1)):
if perso == candidat:
continue
if perso in know[candidat]:
continue
if not oracle.knows(perso, candidat):
return []
return [candidat]
class Inhabitant:
'''
DO NOT MODIFY
'''
id: int = 0
friends = []
def __init__(self, id: int, friends) -> None:
self.id = id
self.friends = friends
def __str__(self):
return f"{self.id} --> {self.friends}"
class Oracle:
'''
DO NOT MODIFY
'''
village = []
size: int = 0
price: int = 0
def __init__(self, village) -> None:
self.village = village
self.size = len(village)-1
self.price = 0
def knows(self, x: int, y: int) -> bool:
self.price += 1
return y in self.village[x].friends
def __str__(self):
result = ""
for person in self.village:
if person is not None:
result += person.__str__()
result += "\n"
return result
def get_oracle_test_with_superstar():
"""
This method returns an oracle representing a village of 3 people in which:
- 1 does not know anybody
- 2 knows 1 and 3
- 3 knows 1
Hence, 1 is a superstar.
"""
return Oracle([
None,
Inhabitant(1, [0]),
Inhabitant(2, [1, 3]),
Inhabitant(3, [1]),
])
def get_oracle_test_without_any_superstar():
"""
This method returns an oracle representing a village of 3 people in which:
- 1 knows 3
- 2 knows 1 and 3
- 3 knows 1
Hence, there isn't any superstar.
"""
return Oracle([
None,
Inhabitant(1, [3]),
Inhabitant(2, [1, 3]),
Inhabitant(3, [1]),
])
if __name__ == '__main__':
'''
you can modify the following in order to test your code
'''
oracle = get_oracle_test_with_superstar()
print(oracle)
superstars = findSuperstar(oracle)
print(f"superstars = {superstars}")
print(f"result found asking {oracle.price} questions")
print("---")
oracle = get_oracle_test_without_any_superstar()
print(oracle)
superstars = findSuperstar(oracle)
print(f"superstars = {superstars}")
print(f"result found asking {oracle.price} questions")