Lihos
Lihos

Reputation: 51

Getting all possible string combinations with Perl

Given a string for example 'rogerdavis' than it should convert it to 'rogerd@vis' or 'rogerdav!s' or 'rogerdavi$' or 'rogerd@v!$' and all possible combination and append it in a file. So basically have to convert 'a' to '@', 's' to '$' and 'i' to '!' and use all possible combinations. This is to be done in Perl.

Pseudocode

This is what came to my mind at first. Please help me because I know there's got to be an easy and simple way to do this thing:

  1. Accept keyword in an array keyword[ ]
  2. Calculate length of the array in length_of_keyword
  3. Scan array keyword[ ] from left to right count =0; for(i=0; i }
  4. Using count to calculate total number of possibilities

    total_poss =0;
    r= 1;
    new_count = count
    for (i = count; i > 0; i--)
    {
        // fact( ) will calculate factorial
        total_poss += fact(new_count)/(fact(r)*fact(new_count - r))  
        r++;
     }
    
    for (k=0; k<total_poss; total_poss++)
       copy array keyword[ ] in temporary array temp[ ];
       for (i=0; i< new_count; i++)
       {
    
           for (j = 0; j< lenght_of_keyword; j++)
           {
               if (temp[i] is equal to 'a' || 'A' || 's' || 'S' || 'i' || 'I' )
               {
                   switch (temp[j])
    
                       case i: tempt[i] = ! ;
                                  if ( modified array is equal to an entry in file)
                                      continue;
                                  else save in file; break;
                       case I: (same as above or we can have function for above code)
                     .
                     .// similarly for all cases
                     .
            }
        }
    }
    

Upvotes: 4

Views: 1043

Answers (3)

Zaid
Zaid

Reputation: 37136

I wanted to give List::Gen a whirl. This problem provided the perfect excuse!


use strict;
use warnings;
use List::Gen;

my %symbol = ( a => '@', A => '@',
               i => '!', I => '!',
               s => '$', S => '$', );  # Symbol table

my $string = 'rogerdavis';
my @chunks = split /(?<=[ais])|(?=[ais])/i, $string;

# Turn into arrayrefs for cartesian function

@chunks = map { $_ =~ /^[ais]$/i ? [ $_, $symbol{$_} ] : [ $_ ] } @chunks;

my $cartesian = cartesian { join '', @_ } @chunks;  # returns a generator

say for @$cartesian;  # or 'say while < $cartesian >'

Output

rogerdavis
rogerdavi$
rogerdav!s
rogerdav!$
rogerd@vis
rogerd@vi$
rogerd@v!s
rogerd@v!$

Upvotes: 6

MkV
MkV

Reputation: 3096

Use glob(3)'s multiple pattern support ({}) by replacing a with {a,@}, s with {s,$} and i with {i,!}, like the following:

my $str = 'rogerdavis';
my $glob = $str;
# set up replacement character map
my %replacements = (a => '@', s => '$', i => '!');
# add uppercase mappings
$replacements{uc $_} = $replacements{$_} for keys %replacements;
# replace 'character' with '{character,replacement}'
$glob =~ s/([asi])/{$1,$replacements{$1}}/ig;
my @list = glob($glob);
print join "\n", @list;
print "\n";
my $count = scalar(@list); 

If the replacement character is a glob(7) metacharacter, then it should be escaped (3 => '\}', e => '\[', for example).

Update: You can replace the [asi] with results of running something like Data::Munge's list2re, f.e.:

my $re = Data::Munge::list2re(keys %replacements);
$glob =~ s/($re)/{$1,$replacements{$1}}/ig;

Upvotes: 5

kjo
kjo

Reputation: 35301

A fairly bare-bones implementation:

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;  # use $i = 1 instead to keep the original $keyword                                                          
              # out of the list of variants                                                                               
  while ( $i < $n_variants ) {
    my @template = @parts;
    my $j = 1;
    my $k = $i;
    for ( 1 .. $n_slots ) {
      $template[ $j ] = $map->{ lc $parts[ $j ] } if $k & 1;
      $j += 2;
      $k >>= 1;
    }
    push @variants, join( '', @template );
    $i++;
  }

  return \@variants;
}

sub main {
  my $keyword = shift @_;
  my $fh = @_ ? ( open( $_[ 0 ], 'a' ) or die $! ) : \*STDOUT;
  print $fh "$_\n" for @{ convert( $keyword ) };
}

main( $ARGV[ 0 ] );

Sample run:

% perl 6995383.pl rogerDaViS
rogerDaViS
rogerD@ViS
rogerDaV!S
rogerD@V!S
rogerDaVi$
rogerD@Vi$
rogerDaV!$
rogerD@V!$

Pardon the lack of comments and lack of error handling (rushed for time), but the basic idea is that if there are n slots that could be replaced, and assuming that there is exactly one possible alternative per slot, then there are 2^n variants (including the original keyword). The bits in (the binary representation of) the $i index are used to keep track of which positions to replace at each iteration of the outer loop. Hence, the iteration with $i == 0 leaves the keyword unchanged. (Therefore, if you don't want this "variant", just shift it out of the returned array.)

This is just a first crack at this. In addition to comments and error handling, I'm sure that, with a bit more thought, this implementation could be improved/tightened significantly.

HTH...

Upvotes: 1

Related Questions