Combinatory Substitution
Not sure if the title is understandable. But I will give an example. Consider a word (well, invented one): noogoo. Now, consider you want to do all possible combinations substituting oo with aa, that is, generate naagoo and noogaa.
The solution I am using, that was idea of my friend Luciano, is as follows:
$word = "noogoo";
push @where, pos($word)-2 while $word =~ /oo/g;
for my $i (@where) {
my $other = $word;
substr($other,$i, 2, "aa");
push @words, $o if $o ne $word;
}
Any better or more efficient idea?
This module uses knuth's combinatorial methods which allow iterations with minimal remembering:
http://search.cpan.org/~fxn/Algorithm-Combinatorics-0.25/Combinatorics.pm
Is it intentional that "naagaa" isn't a listed combination?
I like the question and came up with a recursive solution. The code is posted here: http://gugod.org/2010/07/post-79.html
It probably dose not meet your original requirement but it's fun thinking the solution. Thanks for the question :)
it is like counting in base two:
Yes, I did not want true combinatorial substitutions. Just the words that can be generated with one substitution :)
Rather than presume a constant length match length, and hardcode that 2 twice, consider:
Leonerd, my eyes are hurting :) And, of course I am not using that 2 hard-coded. I have a variable with the match size (I know it is a fixed length match). It won't possible to go with hard-coded length as I need to perform different substitutions approaches :)