women = list(range(1,27)) men = [chr(x + 65) for x in range(26)] n = int(input("Give n ")) men = men[:n] women = ["0"] + women[:n] men_pref = dict() women_pref = dict() for (cur, m) in enumerate(men, start=1): # last man is the same as one before last man if cur == n: cur = cur - 1 # Mk = Wk Wk+1 ... Wn-1 W1 W2 ... Wn prefs = list() k = cur # Wk ... Wn-1 while k < n: prefs.append(women[k]) k += 1 # W1 .. Wn-1 k = 1 while k < cur: prefs.append(women[k]) k += 1 # last woman is always last prefs.append(women[n]) men_pref[m] = prefs # Wk = Wk+1 Wk for (cur, w) in enumerate(women, start=-1): prefs = list() if cur == -1: continue if cur == n-1: print("This is useless") k = 0 while k < n: prefs.append(men[k]) k += 1 women_pref[w] = prefs continue if cur == n - 2: print(f">>>{w}") # W1 Wcur print(f"appending {men[0]}") prefs.append(men[0]) print(f"appending {men[n-1]}") prefs.append(men[n-1]) print(">>>>>>>") print(prefs) print(">>>>>>>") k = 0 while k < n: if k == n-1: k += 1 continue if k == 0: k += 1 continue prefs.append(men[k]) k += 1 women_pref[w] = prefs print(">>>>>>>") print(women_pref[w]) print("<<<<<<<") continue prefs.append(men[cur+1]) prefs.append(men[cur]) # we need n - k more men k = 0 while k < n: if k == cur: k += 1 continue if k == cur + 1: k += 1 continue prefs.append(men[k]) k += 1 women_pref[w] = prefs print(women_pref) print(men_pref)