Skip to content

Instantly share code, notes, and snippets.

@apenney
Created May 18, 2015 15:23
Show Gist options
  • Save apenney/45e689ef53e433720a4d to your computer and use it in GitHub Desktop.
Save apenney/45e689ef53e433720a4d to your computer and use it in GitHub Desktop.
defmodule DFP do
@colors [:red, :white, :blue]
def random_ball(number) do
:random.seed(:erlang.now())
{_, ball} = Enum.fetch(@colors, (:random.uniform(number)-1))
ball
end
# No prior provided
def sorted?([h|_] = list) do
sorted?(list, h)
end
def sorted?([:white|_], :blue) do
false
end
def sorted?([:red|_], prior) when prior in [:blue, :white] do
false
end
def sorted?([], _), do: true
# For the case of blues just keep going
def sorted?([h|t], _) do
sorted?(t, h)
end
def sort(array) when is_list(array) do
Enum.sort(array, fn() -> :red > :white > :blue end)
end
def is_sorted?(array) when is_list(array) do
array
|> Enum.map(fn(x) -> Enum.find_index(@colors, fn(f) -> f == x end) end)
#|> Enum.chunk(2)
|> Enum.all?(fn(a,b) -> a <= b end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment