Skip to content

Instantly share code, notes, and snippets.

@exegeteio
Created July 31, 2023 17:30
Show Gist options
  • Save exegeteio/8d3b781cb5809d9ecda703521a053a0e to your computer and use it in GitHub Desktop.
Save exegeteio/8d3b781cb5809d9ecda703521a053a0e to your computer and use it in GitHub Desktop.
Cassidoo Anagram Detector
#!/usr/bin/env ruby
def anagram?(subject, test)
(test.downcase.chars - subject.downcase.chars).none?
end
tests = {
'evil' => 'vile',
'a gentleman' => 'elegant man',
'Santa' => 'Satan',
'William Shakespeare' => 'I am a weakish speller',
'Tom Marvolo Riddle' => 'I am Lord Voldemort',
'race' => 'racer', # Valid with multuples of letters from the subject.
'racer' => 'race', # Valid with only a single copy of each letter.
'force' => 'farce'
}
tests.each do |s, t|
puts "#{anagram?(s, t) ? 'true ' : 'false'} #{s} => #{t}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment