Last active
September 9, 2016 15:44
-
-
Save dodecaphonic/88dcb9617d1275929127356b709ef049 to your computer and use it in GitHub Desktop.
Intersection Filter
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
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