-
Notifications
You must be signed in to change notification settings - Fork 46
/
celebrity_bruteforce.py
34 lines (31 loc) · 1.14 KB
/
celebrity_bruteforce.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
import random
# Find the first celebrity in a matrix of people. Celebrities know nobody
# Everyone knows the celebrity
def celebrity(matrix):
n = len(matrix)
# For all potential celebrities
for i in range(n):
eliminated = False
# For every other person
for j in range(n):
if not eliminated:
if i == j: # Same person
continue
# If the possible celebrity knows someone, it's not a a celebrity
# If somebody does not know the possible celebrity, its not a celebrity
if matrix[i][j] or not matrix[j][i]:
eliminated = True
if not eliminated:
return i # If no breaks were encountered, we make it here and return the celeb
def main():
matrix = [[random.randint(0, 1)]*5 for i in range(5)]
for i in range(random.randint(0, len(matrix) - 1)):
for j in range(len(matrix)):
matrix[j][i] = 1
matrix[i][j] = 0
for i in range(len(matrix)):
print matrix[i]
celeb = celebrity(matrix)
print "Celebrity:", celeb
if __name__ == "__main__":
main()