Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Not super practical maybe, but using binary XOR is a sneaky way of checking for exact pairs of things (or finding an odd one out!). :) No sorting, and it works with punctuation!
# Detects whether two words are anagrams or notdefis_anagram?(word1,word2)(word1+word2).chars.reduce(0){|acc,curr|acc^curr.ord}==0endputs"stressed is an anagram of desserts: #{is_anagram?('stressed','desserts')}"# => trueputs"happy is an anagram of sad: #{is_anagram?('happy','sad')}"# => falseputs"banana? is an anagram of b?naana: #{is_anagram?('banana?','b?naana')}"# => true
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Oh, good point. This is probably better suited to a “find the one unique letter” type problem. I like the solution that uses ‘uniq’ the best so far, I think.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Not super practical maybe, but using binary XOR is a sneaky way of checking for exact pairs of things (or finding an odd one out!). :) No sorting, and it works with punctuation!
Wouldn't "hello" and "hellooo" be a false positive here? I like the feel of this approach though.
Oh, good point. This is probably better suited to a “find the one unique letter” type problem. I like the solution that uses ‘uniq’ the best so far, I think.