-
Notifications
You must be signed in to change notification settings - Fork 1
Elixir Palindrome Detector #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
72 characters import String;s=downcase replace IO.gets(""),~r/[\s,\.]/,"";s==reverse s It only strips |
I have essentially the same thing, but made it specifically only look at ASCII letters and digits, and assumed import String;n=replace downcase(IO.gets""),~r/\W|_/,"";IO.puts n==reverse n 76 chars with the puts, 68 without. EDIT: |
Shocking how similar! I have 110 chars, but it loops on the input. import String;Enum.each IO.stream(:stdio,:line),fn i->s=downcase replace(i,~r/\W/,"");IO.puts s==reverse s end I think my regex is in the lead so far ;-) twitter handle: @gregvaughn |
IMHO "A man, a plan, a canal -- Panama!" should be a valid palindrome. Same with "Madam, I'm Adam." |
@gvaughn I think the rules say you can have case and punctuation, it shouldn't affect the detector. Therefore "Madam, I'm Adam" etc should be fine. |
@gvaughn I considered |
Oops, I mean |
Just for fun, here is a completely ungolfed palindrome checker without regexps: defmodule Run do
def run do
"abc123ABC!" |> palindrome? |> IO.inspect
"Taco CAT!?" |> palindrome? |> IO.inspect
end
defp palindrome?(string) do
list = string |> to_char_list |> normalize
list == :lists.reverse(list)
end
defp normalize(char_list), do: char_list |> Enum.map(&normalize_char/1) |> Enum.reject(&(&1 == :ignore))
defp normalize_char(char) when char in ?a..?z, do: char
defp normalize_char(char) when char in ?0..?9, do: char
defp normalize_char(char) when char in ?A..?Z, do: char + ?a - ?A
defp normalize_char(_char), do: :ignore
end
Run.run EDIT: No need to loop when we can just compare. 🤦 |
Write Elixir code that takes a string from the standard input and outputs a
true
/false
value indicating whether that string is a palindrome or not.Code will be run from
iex
, when your code is executed it will prompt for user input and on return will output atrue
orfalse
value.Rules
Finally...
Many thanks, Ben
When the puzzle is over I'll write them up on http://elixirgolf.com and link back to your Twitter handle
The text was updated successfully, but these errors were encountered: