Skip to content

Instantly share code, notes, and snippets.

@sauravtom
Last active April 22, 2019 14:29
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 sauravtom/739f2fc0a9ab16812436ca2c109c58f6 to your computer and use it in GitHub Desktop.
Save sauravtom/739f2fc0a9ab16812436ca2c109c58f6 to your computer and use it in GitHub Desktop.
Alchemist Challenge #2
#https://alchemist.camp/episodes/word-count
#filename = String.trim IO.gets "file to count word's from: "
filename = IO.gets("Select file: ") |> String.trim()
choice = IO.gets "What would you like to count ? [lines, words, characters] \n"
case choice |> String.trim() do
"words" -> IO.puts "you chose words"
#words = (File.read!(filename), ~r{\w}) |> String.split()
#words = String.split( File.read!(filename), ~r{(\\n|[^\w'])+} )
words =
File.read!(filename)
|> String.split( ~r{(\\n|[^\w'])+} )
|> Enum.filter(fn x -> x != "" end)
words |> Enum.count() |> IO.puts()
"lines" -> IO.puts "You chose to count lines"
lines = File.read!(filename)
#TODO: make it work
|> String.split(~r{(\n)})
lines |> Enum.count() |> IO.puts()
"characters" -> IO.puts "You chose to count characters"
characters = File.read!(filename)
|> String.split(~r{.})
characters |> Enum.count() |> IO.puts()
_ -> IO.puts "Select from lines, words or characters"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment