Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created August 13, 2014 19:02
Show Gist options
  • Select an option

  • Save iaintshine/ea131c36aadc477cd8a3 to your computer and use it in GitHub Desktop.

Select an option

Save iaintshine/ea131c36aadc477cd8a3 to your computer and use it in GitHub Desktop.
A Ruby program which checks if any anagram of a given string is a palindrome or not
#!/usr/bin/env ruby
# anagram_palindrome.rb
module AnagramPalindrome
ASCII_COUNT = 256
def anagram_of_palindrome?
empty?
histogram = Array.new ASCII_COUNT, 0
self.length.times { |i| histogram[self[i].ord] += 1 }
num_odds = histogram.count &:odd?
num_odds <= 1
end
end
class String
include AnagramPalindrome
end
if __FILE__ == $0
ARGV.each do |sentence|
puts %Q{sentence "#{sentence}" is #{ sentence.anagram_of_palindrome? ? '' : 'not' } an anagram of palindrome}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment