Skip to content

Instantly share code, notes, and snippets.

@tasermonkey
Created March 31, 2015 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasermonkey/e6d9c9912b3284adfa74 to your computer and use it in GitHub Desktop.
Save tasermonkey/e6d9c9912b3284adfa74 to your computer and use it in GitHub Desktop.
is_anagram.rb
#!/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