in reply to Re^4: Gift Exchange Code
in thread Gift Exchange Code

Sure it does. I guess I need to state an assumption - lisp-like lexicals (variables are unmodified by calls that receive them).

p0 = (A, B, C, D); Call match(p0) x1 <= A; p1 = (B, C, D); p.pop x1 <= A; y1 <= B; valid pair, call match(C,D) x2 <= C; p2 = (D); p.pop x2 <= C; y2 <= D; invalid pair - return FAIL x1 <= A; y1 <= C; valid pair, call match(B,D) x2 <= B; p2 = (D); p.pop x2 <= B; y2 <= D; valid pair, call match() p3 = () p3.empty - return SUCCESS r <= ((B, D)); Push results - return SUCCESS r <= ((B, D), (A, C)) Push results - return SUCCESS SUCCESS Results are B&D, A&C

Now, I will give you that the results will be the same every time, and perhaps it would be good to randomize the list every time match is called to try to mix up the results here.

I will also give you that if D could only match with A that this would return FAIL, but if there are pairings to be had, this should find them. Perhaps not the most efficiently, but it should find them.

If I were to implement it in lisp, or even Perl, I would probably return a (STATUS, results) pair, but that is an implementation detail and doesn't really change the algorithm.

Please help me find where I am missing the mistake in the algorithm.