Created
March 31, 2015 02:14
-
-
Save tasermonkey/e6d9c9912b3284adfa74 to your computer and use it in GitHub Desktop.
is_anagram.rb
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 | |
# note that, I am not an expert ruby developer, so there is probably a more ruby way of doing this... | |
require 'awesome_print' | |
def freq(str) | |
str.downcase.split("").map.with_object({}) do |c,h| | |
(h[c] = h.fetch(c, 0) + 1) if c =~ /\w/ | |
end | |
end | |
def is_anagram(str1, str2) | |
c1, c2 = freq(str1), freq(str2) | |
ap [ {name: str1, counts: c1}, {name: str2, counts: c2} ] | |
c1 == c2 | |
end | |
puts "The strings are #{is_anagram(ARGV[0], ARGV[1]) ? "" : "not "}anagrams!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment