in reply to Algorithm Golfing: Intersection of lists
Yet another way to do it:
#!perl use strict; use warnings; my @sets = ([ 0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15, ], [ 0, 3, 7, 8, 10, 11, 13, 14, 16, ], [ 0, 1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 16, ], [ 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 16, ], [ 0, 1, 2, 3, 7, 8, 9, 10, 11, 13, 14, 15, ]); my @intersection; print join("\n", map(join(" ", @$_), @sets)), "\n\n"; while (@sets == grep {@$_ > 0} @sets) { @sets = sort {$a->[0] <=> $b->[0]} @sets; if ($sets[0]->[0] == $sets[-1]->[0]) { push(@intersection, $sets[0]->[0]); shift(@$_) for @sets; } else { my @ne = grep {$sets[0]->[0] != $_->[0]} @sets; my @eq = grep {$sets[0]->[0] == $_->[0]} @sets; shift(@$_) for @eq; @sets = (@eq, @ne); } } print join(" ", @intersection), "\n";
Just my 2 cents, -gjb-
|
|---|