Created
August 13, 2014 19:02
-
-
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
This file contains hidden or 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
| #!/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