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
a ->@
, s->$
,
i-> I
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:
keyword[ ]
length_of_keyword
keyword[ ]
from left to right
count =0;
for(i=0; i
}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
.
}
}
}
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!$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With