Musings of a Gotham City Geek
What is the sound of one blog clapping?
def fudgify_duplicates(points)
points.inject(Array.new) do |fudged, point|
fudged << if (prev = fudged.last) and prev.first >= point.first
[prev.first + 1, point.last]
else
point
end
end
end
if __FILE__ == $PROGRAM_NAME
require "test/unit"
class TestFudgifyDuplicates < Test::Unit::TestCase
def test_no_changes
assert_fudges_to(Array.new, Array.new)
assert_fudges_to([[1, 1], [2, 1]], [[1, 1], [2, 1]])
end
def test_simple_additions
assert_fudges_to([[1, 1], [2, 2]], [[1, 1], [1, 2]])
# the original version fails the following
assert_fudges_to([[1, 1], [2, 2], [3, 3]], [[1, 1], [1, 2], [2, 3]])
end
def test_tricky_less_than_addition
# the original version fails the following
assert_fudges_to([[1, 1], [2, 2], [3, 3]], [[1, 1], [1, 2], [1, 3]])
end
def test_jims_example
assert_fudges_to(
[ [0, 3777], [31, 3254], [33, 2448], [34, 1752],
[36, 389], [56, 259], [100, 726] ],
[ [0, 3777], [31, 3254], [33, 2448], [33, 1752],
[36, 389], [56, 259], [100, 726] ]
)
end
private
def assert_fudges_to(expected, actual)
assert_equal(expected, fudgify_duplicates(actual))
end
end
end