Created
May 18, 2015 15:23
-
-
Save apenney/45e689ef53e433720a4d 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 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