in reply to Python to perl: itertools product to replace a char in string in each possible position
The following python code enables me to take a string of words and replace spaces with hyphens in every possible combination:
The number of every possible combination in the string "convert perl to python" are 8, not 5, or am I missing something?
my $str = 'convert perl-to python'; my $bits =()= $str =~ /[ -]/g; for (0 .. (1<<$bits)-1) { my @l = split//,unpack'b*',pack'C,',$_; (my $out = $str) =~ s/[ -]/(' ','-')[shift @l]/ge; print $out, $/; } __END__ convert perl to python convert-perl to python convert perl-to python convert-perl-to python convert perl to-python convert-perl to-python convert perl-to-python convert-perl-to-python
You get more off that if you use the string "don't convert perl to python" which gives you 16 combinations.
|
|---|