In [400]:
class Male(object):
def __init__(self, id, prefList):
self.prefList = prefList
self.under = 0
self.id = id
def best(self):
return self.prefList[self.under]
def __repr__(self):
return repr(self.id)
In [111]:
# coding: UTF-8
male = {}
male2 = []
for i in range(5):
list = []
for j in range(5):
pref = raw_input("pref> ")
list.append(int(pref))
male.update({i+1: list})
male2 = male.items()
print i+1,"終わり"
male2
Out[111]:
In [270]:
male2 = [(1, [1, 2, 3, 4, 51]),
(2, [1, 1, 1, 1, 1]),
(3, [1, 1, 1, 1, 1]),
(4, [1, 1, 1, 1, 1]),
(5, [1, 1, 1, 1, 1])]
In [382]:
males = []
for i in range(0,5):
a = male2[i][1]
males.append(Male(i+1,a))
males
Out[382]:
In [ ]:
#Male(())の入力になってはいけない。
In [372]:
Out[372]:
In [135]:
for i in range(0,5):
print(males[i].prefList)
In [356]:
class Female(object):
def __init__(self, id, prefList):
self.prefList = prefList
self.held = None
self.currentMales = 0
self.id = id
def __repr__(self):
return repr(self.id)
def reject(self):
if len(self.currentMales) == 0:
return set()
if self.held is not None:
self.currentMales.add(self.held)
self.held = min(self.currentMales, key=lambda Males: self.prefList.index(males.id))
rejected = self.currentMales - set([self.held])
self.currentMales = set()
return rejected
In [261]:
female = {}
for i in range(5):
list = []
for j in range(5):
pref = raw_input("pref> ")
list.append(int(pref))
female.update({i+1: list})
female2 = female.items()
print i+1,"終わり"
female2
Out[261]:
In [357]:
female2 = [(1, [1, 2, 3, 1, 2]),
(2, [3, 1, 2, 3, 1]),
(3, [2, 3, 1, 2, 3]),
(4, [1, 2, 3, 1, 2]),
(5, [3, 1, 1, 1, 1])]
In [358]:
females = []
for i in range(0,5):
a = female2[i][1]
females.append(Female(i+1,a))
females
Out[358]:
In [195]:
# monogamousStableMarriage: [Suitor], [Suited] -> {Suitor -> Suited}
# construct a stable (monogamous) marriage between suitors and suiteds
def Marriage(males, females):
unassigned = set(males)
while len(unassigned) > 0:
for males in unassigned:
females[males.preference()].currentMales.add(males)
unassigned = set()
for females in females:
unassigned |= females.reject()
for males in unassigned:
males.under += 1
return dict([(suited.held, suited) for suited in suiteds])
In [437]:
males = []
for i in range(0,5):
a = male2[i][1]
males.append(Male(i+1,a))
males
Out[437]:
In [ ]:
mprefer = [[1, 2, 3, 4],
[3, 2, 1, 4],
[1, 2, 4, 3],
[3, 1, 4, 2]]
fprefer = [[1, 2, 3, 4],
[2, 1, 4, 3],
[2, 3, 1, 4],
[1, 4, 3, 2]]
In [423]:
single = set(males)
while len(single) > 0:
for males in single:
for males in females[males.best()].prefList:
if females[males.best()].currentMales == 0:
females[males.best()].currentMales = males
elif females[males.best()].prefList.index(females[males.best()].currentMales) < females[loved].prefList.index(i):
females[males.best()].currentMales = 0
females[males.best()].currentMales = males
else:
males.under +=1
return
In [415]:
males[1].best()
Out[415]:
In [438]:
single = set(males)
for males in single:
print(males.best())
In [313]:
females[females[1].currentMales]
In [323]:
set([9,10,3])
Out[323]:
In [329]:
males[1].prefList.index(1)
Out[329]:
In [344]:
a = [1,5,3,4,5]
In [334]:
a.index(4)
Out[334]:
In [337]:
a = []
In [338]:
a
Out[338]:
In [352]:
i = 1
if i in females[males[1].preference()].prefList:
print(7)
else:
print(100)
In [349]:
females[males[1].preference()].prefList
Out[349]:
In [365]:
males[0].prefList[0]
Out[365]:
In [376]:
males
Out[376]:
In [ ]: