from math import sqrt, isclose loi = [False, False, True, True] phu = { "S1": [True, True, True, True], "S2": [False, False, True, True], "S3": [True, False, True, False], } def ochiai(da_chay): ef = sum(f and r for f, r in zip(loi, da_chay, strict=True)) ep = sum(not f and r for f, r in zip(loi, da_chay, strict=True)) tong_loi = sum(loi) mau = sqrt(tong_loi * (ef + ep)) return ef / mau if mau else 0 diem = {dong: ochiai(cot) for dong, cot in phu.items()} assert isclose(diem["S1"], 1 / sqrt(2)) assert diem["S2"] == 1 assert diem["S3"] == 0.5 thu_tu = sorted(diem, key=lambda dong: (-diem[dong], dong)) assert thu_tu == ["S2", "S1", "S3"] print([(dong, round(diem[dong], 3)) for dong in thu_tu])