Skip to content

Instantly share code, notes, and snippets.

@mkb
Created August 19, 2012 02:50
Show Gist options
  • Select an option

  • Save mkb/3391254 to your computer and use it in GitHub Desktop.

Select an option

Save mkb/3391254 to your computer and use it in GitHub Desktop.
Anagrams
#!/usr/bin/env ruby
# Props to @watersb, @ussintega, and @climagic
one = "Mitt Romney and Paul Ryan"
two = "My ultimate Ayn Rand porn"
def sorted_letters(sentence)
sentence.downcase.gsub(/\s+/, '').chars.sort.join
end
def is_anagram?(first, second)
sorted_letters(first) == sorted_letters(second)
end
puts is_anagram?(one, two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment