In [41]:
def da3(P,Q):
import numpy as np
import sys
if isinstance(Q, list) != True and isinstance(Q,np.ndarray) != True:
print "Input Error!!"
sys.exit()
if isinstance(P, list):
P = np.array(P)
Q = np.array(Q)
elif isinstance(P, np.ndarray):
print "ndarray"
else:
print "Input Error!!"
sys.exit()
N=P.shape[0]
G=Q.shape[0]
S=np.tile([-1],N)
free=range(N)
n=0
while free:
print S
print n
"""
上のシャープを外すことで男性の婚約状況の一周ごとの推移を表示することができます
正しくない結果が返ってきている疑いのある時に様子を見るため使いましょう
"""
A=free.pop()
F=P[A,:][n]
if F==G:
S[A]=G
n=0
else:
for J in range(N+1):
if Q[F,:][J]==A:
D=J
if Q[F,:][J]==N:
E=J
if E<D:
free.append(A)
n+=1
else:
if F not in S:
S[A]=F
n=0
else:
for y in range(N):
if S[y]==F:
M=y
for z in range(N+1):
if Q[F,:][z]==M:
Z=z
if Q[F,:][z]==A:
W=z
if W<Z:
S[A]=F
S[M]=-1
free.append(M)
n=0
else:
free.append(A)
n+=1
if len(free)==0:
s=list(S)
L=np.tile([N],G)
l=list(L)
for u in s:
if u==G: continue
else:
v=s.index(u)
l[u]=v
return s,l
else: continue
In [42]:
import numpy as np
a=np.random.permutation(5)
b=np.random.permutation(5)
c=np.random.permutation(5)
d=np.random.permutation(5)
e=np.random.permutation(5)
f=np.random.permutation(5)
g=np.random.permutation(5)
h=np.random.permutation(5)
i=np.random.permutation(5)
j=np.random.permutation(5)
n_pref=np.vstack((a,b,c,d,e))
m_pref=np.vstack((f,g,h,i,j))
In [43]:
n_pref.shape[0]
Out[43]:
In [44]:
da3(m_pref,n_pref)
In [128]:
m_pref
Out[128]:
In [129]:
n_pref
Out[129]:
In [187]:
list(range(8))
Out[187]:
In [32]:
def da3(P,Q):
import numpy as np
import sys
np.asarray(P)
np.asarray(Q)
N=P.shape[0]
G=Q.shape[0]
S=np.tile([-1],N)
free=range(N)
n=0
while free:
#print S
#print n
"""
上のシャープを外すことで男性の婚約状況の一周ごとの推移を表示することができます
正しくない結果が返ってきている疑いのある時に様子を見るため使いましょう
"""
A=free.pop()
F=P[A,:][n]
if F==G:
S[A]=G
n=0
else:
for J in range(N+1):
if Q[F,:][J]==A:
D=J
if Q[F,:][J]==N:
E=J
if E<D:
free.append(A)
n+=1
else:
if F not in S:
S[A]=F
n=0
else:
for y in range(N):
if S[y]==F:
M=y
for z in range(N+1):
if Q[F,:][z]==M:
Z=z
if Q[F,:][z]==A:
W=z
if W<Z:
S[A]=F
S[M]=-1
free.append(M)
n=0
else:
free.append(A)
n+=1
if len(free)==0:
s=list(S)
L=np.tile([N],G)
l=list(L)
for u in s:
if u==G: continue
else:
v=s.index(u)
l[u]=v
return s,l
else: continue
#リスト内包表記は早い
In [33]:
da3(m_pref,n_pref)
In [2]:
a={}
for i in range(5):
a[i]=[]
In [3]:
a
Out[3]:
In [ ]: