Code:
class Rectangle
attr_accessor :p1, :p2
def initialize(p1,p2)
@p1 = p1
@p2 = p2
end
def overlap_rectangle(rect)
xs = {
my1: p1[0],
my2: p2[0],
other1: rect.p1[0],
other2: rect.p2[0]
}
ys = {
my1: p1[1],
my2: p2[1],
other1: rect.p1[1],
other2: rect.p2[1]
}
x_interval = interval(xs)
y_interval = interval(ys)
p1 = [x_interval[0], y_interval[0]]
p2 = [x_interval[1], y_interval[1]]
Rectangle.new(p1,p2)
end
def interval(points)
sorted_keys = sorted_keys_by_value(points)
interval = [0,0]
if no_overlap(sorted_keys, points)
interval = [0,0]
elsif share_edges(points)
interval = min_max(points)
else
sorted_keys.each_cons(3) do |keys|
if keys[1].to_s[0...-1] != keys[2].to_s[0...-1]
interval = [points[keys[1]], points[keys[0]]]#[hi,low]
end
end
end
interval
end
def sorted_keys_by_value(points)
sorted_keys = []
points.values.sort.uniq.reverse.each do |val|
#simple points.key(val) fails when keys share a value so do this
points.select{|k,v| v == val}.each do |k,v|
sorted_keys << k
end
end
sorted_keys
end
def min_max(points)
[points.values.min, points.values.max]
end
def share_edges(points)
points.values.uniq.length == 2
end
def no_overlap(sorted_points,points)
whos = sorted_points.map {|p| p.to_s[0...-1]}
(whos[0] == whos[1]) && (whos[2] == whos[3]) \
&& points.values.uniq.length > 2
end
end
Code:
require 'minitest/autorun'
require_relative 'rectangle'
class RectangleTest < MiniTest::Unit::TestCase
def test_1
a = Rectangle.new([2,3],[5,8])
b = Rectangle.new([4,4],[9,9])
n = a.overlap_rectangle(b)
assert_equal [4,4], n.p1
assert_equal [5,8], n.p2
end
def test_2
a = Rectangle.new([0,0],[9,9])
b = Rectangle.new([2,3],[4,7])
n = a.overlap_rectangle(b)
assert_equal [2,3], n.p1
assert_equal [4,7], n.p2
end
def test_3
a = Rectangle.new([0,0],[9,9])
b = Rectangle.new([2,3],[9,9])
n = a.overlap_rectangle(b)
assert_equal [2,3], n.p1
assert_equal [9,9], n.p2
end
def test_4
a = Rectangle.new([1,3],[9,9])
b = Rectangle.new([0,0],[9,9])
n = a.overlap_rectangle(b)
assert_equal [1,3], n.p1
assert_equal [9,9], n.p2
end
def test_5
a = Rectangle.new([1,3],[9,9])
b = Rectangle.new([-2,-8],[5,8])
n = a.overlap_rectangle(b)
assert_equal [1,3], n.p1
assert_equal [5,8], n.p2
end
def test_6
a = Rectangle.new([1,3],[9,9])
b = Rectangle.new([-1,5],[7,7])
n = a.overlap_rectangle(b)
assert_equal [1,5], n.p1
assert_equal [7,7], n.p2
end
def test_7
a = Rectangle.new([1,3],[4,4])
b = Rectangle.new([5,5],[7,7])
n = a.overlap_rectangle(b)
assert_equal [0,0], n.p1
assert_equal [0,0], n.p2
end
def test_jj
a = Rectangle.new([1,3],[4,4])
b = Rectangle.new([2,2],[4,4])
n = a.overlap_rectangle(b)
assert_equal [2,3], n.p1
assert_equal [4,4], n.p2
end
def test_same
a = Rectangle.new([2,2],[4,4])
b = Rectangle.new([2,2],[4,4])
n = a.overlap_rectangle(b)
assert_equal [2,2], n.p1
assert_equal [4,4], n.p2
end
def test_almost_same
a = Rectangle.new([3,2],[4,4])
b = Rectangle.new([2,2],[4,4])
n = a.overlap_rectangle(b)
assert_equal [4,4], n.p2
assert_equal [3,2], n.p1
end
def test_almost_same2
a = Rectangle.new([2,3],[4,4])
b = Rectangle.new([2,2],[4,4])
n = a.overlap_rectangle(b)
assert_equal [4,4], n.p2
assert_equal [2,3], n.p1
end
def test_almost_same3
a = Rectangle.new([2,2],[4,3])
b = Rectangle.new([2,2],[4,4])
n = a.overlap_rectangle(b)
assert_equal [2,2], n.p1
assert_equal [4,3], n.p2
end
def test_almost_same4
a = Rectangle.new([2,2],[3,4])
b = Rectangle.new([2,2],[4,4])
n = a.overlap_rectangle(b)
assert_equal [2,2], n.p1
assert_equal [3,4], n.p2
end
end
I think this works. Feel free to critique, I've been posting pure **** to exercism.io lately and realizing how much I actually suck at this.
This was also a lot harder than I thought it'd be btw. I wouldn't feel too bad about flubbing this and it's the exact type of problem you can improve on with practice/exposure to similar problems.