Skip to content

Instantly share code, notes, and snippets.

@dodecaphonic
Last active September 9, 2016 15:44
Show Gist options
  • Save dodecaphonic/88dcb9617d1275929127356b709ef049 to your computer and use it in GitHub Desktop.
Save dodecaphonic/88dcb9617d1275929127356b709ef049 to your computer and use it in GitHub Desktop.
Intersection Filter
require "minitest/autorun"
class TestIntersectionFilter < Minitest::Test
def test_data
[[1, 2, 3], [1, 2, 4], [1, 2, 5],
[1, 5, 7], [1, 5, 8], [1, 5, 9]]
end
def test_that_it_works
filtered_data = IntersectionFilter.filter(test_data)
assert_equal [[1, 2, 3], [1, 5, 7]], filtered_data
end
end
module IntersectionFilter
def self.filter(test_data, max = 2)
test_data.reduce([]) { |filtered, to_check|
filtered.any? { |set| (set & to_check).size >= max } ?
filtered : filtered + [to_check]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment