Introduction-to-Discrete-Mathematics-for-Computer-Science-Specialization
Introduction-to-Discrete-Mathematics-for-Computer-Science-Specialization copied to clipboard
can the code for the Algorithm quiz be attached in a separate txt file
Please give the link which code you want ?
https://github.com/ChanchalKumarMaji/Introduction-to-Discrete-Mathematics-for-Computer-Science-Specialization/blob/master/Introduction%20to%20Graph%20Theory/Week5/Quiz:%20Algorithm.png
Thank you so much
On Thu, Jun 4, 2020, 11:33 PM Chanchal Kumar Maji [email protected] wrote:
Please give the link which code you want ?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ChanchalKumarMaji/Introduction-to-Discrete-Mathematics-for-Computer-Science-Specialization/issues/2#issuecomment-639014713, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMHBWRXALEUY2FVNYFXRLI3RU7OWTANCNFSM4NS2BJBA .
def stableMatching(n, menPreferences, womenPreferences):
# Do not change the function definition line.
# Initially, all n men are unmarried
unmarriedMen = list(range(n))
# None of the men has a spouse yet, we denote this by the value None
manSpouse = [None] * n
# None of the women has a spouse yet, we denote this by the value None
womanSpouse = [None] * n
# Each man made 0 proposals, which means that
# his next proposal will be to the woman number 0 in his list
nextManChoice = [0] * n
# While there exists at least one unmarried man:
while unmarriedMen:
# Pick an arbitrary unmarried man
he = unmarriedMen[0]
# Store his ranking in this variable for convenience
hisPreferences = menPreferences[he]
# Find a woman to propose to
she = hisPreferences[0]
# Store her ranking in this variable for convenience
herPreferences = womenPreferences[she]
# Find the present husband of the selected woman (it might be None)
#currentHusband = womanSpouse[she]
# Write your code here
# Now "he" proposes to "she".
# Decide whether "she" accepts, and update the following fields
# 1. manSpouse
# 2. womanSpouse
# 3. unmarriedMen
# 4. nextManChoice
if he in herPreferences:
manSpouse[he] = she
if womanSpouse[she] != None:
manSpouse[womanSpouse[she]] = None
unmarriedMen.append(womanSpouse[she])
womanSpouse[she] = he
l = []
for e in herPreferences:
if e == he:
womenPreferences[she] = l
break
else:
l.append(e)
unmarriedMen.remove(he)
menPreferences[he].remove(she)
# Note that if you don't update the unmarriedMen list,
# then this algorithm will run forever.
# Thus, if you submit this default implementation,
# you may receive "SUBMIT ERROR".
return manSpouse
# You might want to test your implementation on the following two tests:
# assert(stableMatching(1, [ [0] ], [ [0] ]) == [0])
# assert(stableMatching(2, [ [0,1], [1,0] ], [ [0,1], [1,0] ]) == [0, 1])
Hello. Did you have the pdf of the book that was in the specialization? Can you upload it?