Skip to content

Instantly share code, notes, and snippets.

@slofurno
Last active March 30, 2017 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slofurno/d9c27dd43ae58de2a834187238a24d19 to your computer and use it in GitHub Desktop.
Save slofurno/d9c27dd43ae58de2a834187238a24d19 to your computer and use it in GitHub Desktop.
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