-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockinggroup.py
133 lines (101 loc) · 3.71 KB
/
blockinggroup.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
# -*- coding: utf-8 -*-
"""
Created on Fri May 02 21:34:46 2014
@author: Evan
"""
import itertools
import sys
print "Enter Max Rating"
maxRating = int(raw_input())
print "Enter Names of People (separated by space): "
names = raw_input().split()
numPeople = len(names)
ratings = [[0 for j in range(numPeople)] for i in range(numPeople)]
for i in range(numPeople):
print "-------------------------------------------------"
print "The names IN ORDER are: " + str(names)
print "%s enter your utility (0-%d inclusive) in the order of the names above:" % (names[i], maxRating)
ratings[i] = map(int, raw_input().split())
for i in ratings[i]:
if i < 0 or i > maxRating:
print "YOU BIG FAT CHEATER!!!"
sys.exit()
if len(ratings[i]) != numPeople:
print "You didn't type the correct number of people!! START OVER!"
sys.exit()
else:
for i in range(30):
print ""
print "The previous entry is now hidden"
print "-------------------------------------------------"
nC2 = numPeople * (numPeople - 1) / 26
nC3 = numPeople * (numPeople - 1) * (numPeople -2) / 6
nC4 = numPeople * (numPeople - 1) * (numPeople - 2) * (numPeople - 3) / 24
bank = [i for i in range(numPeople)]
weights = []
combinations = []
counter = 0
withs = [[] for i in range(numPeople)]
for i in itertools.combinations(bank, 2):
first = i[0]
second = i[1]
combinations.append([first,second])
for i in range(numPeople):
if i in [first,second]:
withs[i].append(counter)
weights.append(ratings[first][second] + ratings[second][first])
counter += 1
for i in itertools.combinations(bank, 3):
first = i[0]
second = i[1]
third = i[2]
combinations.append([first,second,third])
for i in range(numPeople):
if i in [first,second, third]:
withs[i].append(counter)
weights.append((ratings[first][second] + ratings[second][first] + ratings[first][third] + ratings[third][first] + ratings[second][third] + ratings[third][second])/2)
counter += 1
for i in itertools.combinations(bank, 4):
first = i[0]
second = i[1]
third = i[2]
fourth = i[3]
combinations.append([first,second,third,fourth])
for i in range(numPeople):
if i in [first,second, third, fourth]:
withs[i].append(counter)
weights.append((ratings[first][second] + ratings[second][first] +ratings[first][third] +ratings[third][first] +ratings[first][fourth] +ratings[fourth][first] +ratings[second][third] +ratings[third][second] +ratings[second][fourth] +ratings[fourth][second] +ratings[third][fourth] +ratings[fourth][third])/3)
counter += 1
graph = [[] for i in range(len(combinations))]
for i in range(len(graph)):
sets = []
for j in combinations[i]:
sets.append(set(withs[j]))
graph[i] = (list(set.union(*sets)))
def wis(current):
if len(current) == 0:
return [0,[]]
first = current[0]
A = current[1:len(current)]
B = current[1:len(current)]
# Use that node
for i in graph[first]:
if i in A:
A.remove(i)
use = wis(A)
use_value = weights[first] + use[0]
# Don't use that node
dontuse = wis(B)
dontuse_value = dontuse[0]
if use_value > dontuse_value:
return [use_value, use[1] + [first]]
else:
return [dontuse_value, dontuse[1]]
arrangement= wis([i for i in range(len(combinations))])[1]
def printresult(i):
result = combinations[i]
print "----------------Rooming Together-----------------"
for i in result:
print names[i]
for i in arrangement:
printresult(i)