Last active
December 14, 2015 12:59
-
-
Save BastienClement/5090490 to your computer and use it in GitHub Desktop.
DS Homework 2, ex 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local D0 = {9,6,1,3,0,4,5,7,8,2} | |
local function D_next(D) | |
local Dn = {} | |
for i = 1, #D do | |
Dn[i] = (D[i] + 1) % 10 | |
end | |
return Dn | |
end | |
local D1 = D_next(D0) | |
local D2 = D_next(D1) | |
local D3 = D_next(D2) | |
local D4 = D_next(D3) | |
local D5 = D_next(D4) | |
local function D_str(D) | |
local s = "" | |
for i = 1, #D do | |
s = s .. tostring(D[i]) | |
end | |
return s | |
end | |
local function L(D) | |
local Ln = {} | |
for i = 1, #D do | |
if D[i] > 0 and D[i] < 6 then | |
Ln[#Ln + 1] = D[i] | |
end | |
end | |
return Ln | |
end | |
local L1 = L(D1) | |
local L2 = L(D2) | |
local L3 = L(D3) | |
local L4 = L(D4) | |
local L5 = L(D5) | |
print("D1 = " .. D_str(D1), "L1 = " .. D_str(L1)) | |
print("D2 = " .. D_str(D2), "L2 = " .. D_str(L2)) | |
print("D3 = " .. D_str(D3), "L3 = " .. D_str(L3)) | |
print("D4 = " .. D_str(D4), "L4 = " .. D_str(L4)) | |
print("D5 = " .. D_str(D5), "L5 = " .. D_str(L5)) | |
print() | |
local G1 = L1 | |
local G2 = L2 | |
local G3 = L1 | |
local G4 = L2 | |
local G5 = L1 | |
local B1 = L3 | |
local B2 = L3 | |
local B3 = L3 | |
local B4 = L4 | |
local B5 = L5 | |
print("G1 = " .. D_str(G1), "B1 = " .. D_str(B1)) | |
print("G2 = " .. D_str(G2), "B2 = " .. D_str(B2)) | |
print("G3 = " .. D_str(G3), "B3 = " .. D_str(B3)) | |
print("G4 = " .. D_str(G4), "B4 = " .. D_str(B4)) | |
print("G5 = " .. D_str(G5), "B5 = " .. D_str(B5)) | |
print() | |
local GPref = { G1, G2, G3, G4, G5 } | |
local BPref = { B1, B2, B3, B4, B5 } | |
local function hasUnmatched(P, M) | |
for i = 1, #P do | |
if not M[i] then | |
return true | |
end | |
end | |
return false | |
end | |
local function likeBetter(P, I1, I2) | |
for i = 1, #P do | |
if P[i] == I1 then | |
return true | |
elseif P[i] == I2 then | |
return false | |
end | |
end | |
return false | |
end | |
local function match(P1, P2) | |
local M1 = {} | |
local M2 = {} | |
while hasUnmatched(P1, M1) do | |
for i = 1, #P1 do | |
if not M1[i] then | |
local prime = P1[i][1] | |
if not M2[prime] then | |
M1[i] = prime | |
M2[prime] = i | |
else | |
local o = M2[prime] | |
if likeBetter(P2[prime], i, o) then | |
M1[i] = prime | |
M2[prime] = i | |
M1[o] = nil | |
for i = 1, #P1[o] do | |
if P1[o][i] == prime then | |
table.remove(P1[o], i) | |
break | |
end | |
end | |
else | |
table.remove(P1[i], 1) | |
end | |
end | |
end | |
end | |
end | |
for i = 1, #M1 do | |
print(i .. " <-> " .. M1[i]) | |
end | |
end | |
print("Female optimal") | |
match(GPref, BPref) | |
print() | |
print("Male optimal") | |
match(BPref, GPref) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment