Created
October 15, 2019 04:28
-
-
Save akinomaeni/0dc2b33a1413b22071a80a3bf6ae3961 to your computer and use it in GitHub Desktop.
Do ranges overlap?
This file contains hidden or 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
| # frozen_string_literal: true | |
| # 12345678 | |
| # ---- 3..6 RANGE | |
| # --- 2..4 true | |
| # ------ 2..7 true | |
| # -- 4..5 true | |
| # --- 5..7 true | |
| # -- 1..2 false | |
| # -- 7..8 false | |
| RANGE = 3..6 | |
| examples = [ | |
| [2..4, true], | |
| [2..7, true], | |
| [4..5, true], | |
| [5..7, true], | |
| [1..2, false], | |
| [7..8, false], | |
| ] | |
| def orverlap?(a, b) | |
| a.first < b.last && b.first < a.last | |
| end | |
| examples.each do |range, expectation| | |
| puts "---" | |
| puts orverlap?(range, RANGE) == expectation | |
| puts orverlap?(RANGE, range) == expectation | |
| end |
This file contains hidden or 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
| $ ruby overlap.rb | |
| --- | |
| true | |
| true | |
| --- | |
| true | |
| true | |
| --- | |
| true | |
| true | |
| --- | |
| true | |
| true | |
| --- | |
| true | |
| true | |
| --- | |
| true | |
| true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment