Skip to content

Instantly share code, notes, and snippets.

@akinomaeni
Created October 15, 2019 04:28
Show Gist options
  • Select an option

  • Save akinomaeni/0dc2b33a1413b22071a80a3bf6ae3961 to your computer and use it in GitHub Desktop.

Select an option

Save akinomaeni/0dc2b33a1413b22071a80a3bf6ae3961 to your computer and use it in GitHub Desktop.
Do ranges overlap?
# 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
$ 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