emson / elixirgolf

Elixir Golf web site
MIT License
3 stars 1 forks source link

Elixir Palindrome Detector #7

Open emson opened 8 years ago

emson commented 8 years ago

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 a true or false value.

iex> --enter your code here--
level
true

Rules

Finally...

  1. Please add your solutions as comments to this Github issue
  2. Remember to add your Twitter handle (I will link to it if you get a mention)

Many thanks, Ben
When the puzzle is over I'll write them up on http://elixirgolf.com and link back to your Twitter handle

michalmuskala commented 8 years ago

72 characters

import String;s=downcase replace IO.gets(""),~r/[\s,\.]/,"";s==reverse s

It only strips ., as punctuation

henrik commented 8 years ago

I have essentially the same thing, but made it specifically only look at ASCII letters and digits, and assumed IO.puts:

import String;n=replace downcase(IO.gets""),~r/\W|_/,"";IO.puts n==reverse n

76 chars with the puts, 68 without.

EDIT: /[^a-z\d]/ -> /\W|_/

gvaughn commented 8 years ago

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

gvaughn commented 8 years ago

IMHO "A man, a plan, a canal -- Panama!" should be a valid palindrome. Same with "Madam, I'm Adam."

emson commented 8 years ago

@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.

henrik commented 8 years ago

@gvaughn I considered \W but wanted to be quite strict about the input and exclude underscores. But come to think of it, /[^\w_]/ would achieve that with fewer chars than what I did.

henrik commented 8 years ago

Oops, I mean /[\W_]/. Or to shave off one character, /\W|_/.

henrik commented 8 years ago

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. :facepalm: