The implementation of fairly bare bones:
sub convert { my $keyword = shift @_; my $map = @_ ? $_[ 0 ] : \%MAP; my @parts = do { my $regex = do { my $letters = join('', keys %$map); qr/([$letters])/i; }; split($regex, $keyword, -1); }; my $n_slots = ( -1 + scalar @parts )/2; my $n_variants = 2 ** $n_slots; my @variants; my $i = 0;
Run Example:
% perl 6995383.pl rogerDaViS rogerDaViS rogerD@ViS rogerDaV!S rogerD@V !S rogerDaVi$ rogerD@Vi $ rogerDaV!$ rogerD@V !$
Forgive the lack of comments and the lack of error handling (hurried over time), but the main idea is that if there are n slots that can be replaced, and if we assume that there is only one possible alternative for each slot, then there are 2 ^ n options (including the original keyword). The bits in the (binary representation) of the $i index are used to track which positions to replace at each iteration of the outer loop. Therefore, iterating with $i == 0 leaves the keyword unchanged. (Therefore, if you do not need this βoptionβ, just shift it from the returned array.)
This is just the first crack. In addition to comments and error handling, I'm sure that with a little thought, this implementation can be greatly improved / tightened.
NTN ...
source share