Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can the code for the Algorithm quiz be attached in a separate txt file #2

Open
nairMadhav opened this issue Jun 4, 2020 · 4 comments

Comments

@nairMadhav
Copy link

No description provided.

@ChanchalKumarMaji
Copy link
Owner

Please give the link which code you want ?

@nairMadhav
Copy link
Author

nairMadhav commented Jun 4, 2020 via email

@ChanchalKumarMaji
Copy link
Owner

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])

@DimaMirana
Copy link

Hello. Did you have the pdf of the book that was in the specialization? Can you upload it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants