Last active
March 30, 2017 02:51
-
-
Save slofurno/d9c27dd43ae58de2a834187238a24d19 to your computer and use it in GitHub Desktop.
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
defmodule S do | |
def test do | |
a = [[1, 3], [2, 6], [8, 10], [7, 11]] | |
[[1,6], [7,11]] = interval(a) | |
a = [[5,12],[8,10]] | |
[[5,12]] = interval(a) | |
a = [[1,6], [6,10]] | |
^a = interval(a) | |
:ok | |
end | |
def interval(xs) do | |
[[a,b]|xs] = Enum.sort(xs, fn([a, _], [b, _]) -> a <= b end) | |
merge(xs, a, b, []) | |
end | |
def merge([], a, b, ret), do: [[a,b]|ret] |> Enum.reverse | |
def merge([[a1,b1]|xs], a, b, ret) do | |
case a1 < b do | |
true -> merge(xs, a, max(b1, b), ret) | |
false -> merge(xs, a1, b1, [[a,b]|ret]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment