On Oct 18, 2:08 am, Brian Adkins <lojicdot... / gmail.com> wrote:
> On Oct 17, 11:48 pm, Michael Linfield <globyy3... / hotmail.com> wrote:
>
> > Making an form of an anagram solver. My approach would be the code
> > listed below but there has to be a cleaner way to do it.
>
> As others have mentioned, simply generating all permutations may not
> be the best way to solve an anagram, but here's a recursive
> permutation generator:
>
> def words chars, result='', &b
>   if chars.empty?
>     yield result
>   else
>     chars.length.times do |i|
>       c = (x = chars.clone).slice!(i)
>       words(x, result + c, &b)
>     end
>   end
> end
>
> word = 'dog';
> words(word.split('')) {|s| puts s }

I was curious about the code using the facets gem, so I ran a few
benchmarks. The above naive recursive code is over 3 times faster on
an 8 character word. I'm sure this is due in part to the above code
being quite specific and the facets code needing to be more general,
but that still seems like a large factor.