Skip to content

Instantly share code, notes, and snippets.

@mcelaney
Last active March 2, 2017 14:34
Show Gist options
  • Save mcelaney/fc9f9608d4823bcbe34f4887379e67b5 to your computer and use it in GitHub Desktop.
Save mcelaney/fc9f9608d4823bcbe34f4887379e67b5 to your computer and use it in GitHub Desktop.
PhillyDev Slack #daily_programmer for March 1, 2017
defmodule AplhaBet do
@moduledoc """
The word `chimps` is an example of a word that's in alphabetical order. Write
a program that prints out if the word is in alphabetical order. For bonus
make it test backwards too!
"""
@doc """
Returns true if the given string is in alphabetical or reverse-alphabetical
order
# Examples
iex> AplhaBet.alphabetical?("chimps")
true
iex> AplhaBet.alphabetical?("spoonfeed")
true
iex> AplhaBet.alphabetical?("asked")
false
"""
@spec alphabetical?(String.t) :: boolean
def alphabetical?(word) do
ordered_chars = sorted_characters(word)
ordered_chars
|> Enum.join
|> Kernel.==(word)
or
ordered_chars
|> Enum.reverse
|> Enum.join
|> Kernel.==(word)
end
@spec sorted_characters(String.t) :: list(String.t)
defp sorted_characters(word) do
word
|> String.graphemes
|> Enum.sort
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment