Last active
April 22, 2019 14:29
-
-
Save sauravtom/739f2fc0a9ab16812436ca2c109c58f6 to your computer and use it in GitHub Desktop.
Alchemist Challenge #2
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
#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