There's more than one way to do things PerlMonks

### Re^2: How can one get all possible combinations of a string without changing positions & using window size?

by supriyoch_2008 (Monk)
 on Apr 21, 2013 at 04:45 UTC ( #1029708=note: print w/replies, xml ) Need Help??

Hi hdb,

My problem is similar to the following exercise given in the English Grammar book of primary school students. Construct all possible sentences from the given table using all blocks (4) and count total numbers of such sentences.

Here goes the Table with 4 blocks:

```----------------------------------------
I   | play       |         | at home.  |
You | donot play | soccer  | at school.|
We  |            |         |           |
----------------------------------------

Result:
No. of possible sentences: 3X2x1x2=12

Sentences are:

I play soccer at home.
I play soccer at school.
I donot play soccer at home.
I donot play soccer at school.
You play soccer at home.
You play soccer at school.
You donot play soccer at home.
You donot play soccer at school.
We play soccer at home.
We play soccer at school.
We donot play soccer at home.
We donot play soccer at school.

I want all possible combinations of 10-letter from the string "ATATGCGCAT" without changing positions of letters in the actual string, where say A is of 3 levels, T at 2 levels, G at 2 levels and C at 1 level. By 3 levels of A I mean that A has A1,A2 & A3. Likewise, T1,T2 for T; G1,G2 for G and C1 for C. I want to use window size in the script to break a bigger string into smaller fragments & obtain all possible combinations for each fragment & then concatenate them. I hope this will help perl monks to understand my problem in a better way. I am sorry because I could not possibly present the problem nicely in the thread entitled "How can one get all possible combinations of a string without changing positions?" yesterday.

• Comment on Re^2: How can one get all possible combinations of a string without changing positions & using window size?

Replies are listed 'Best First'.
Re^3: How can one get all possible combinations of a string without changing positions & using window size?
by Kenosis (Priest) on Apr 21, 2013 at 07:01 UTC

Perhaps the following will be helpful as a pattern for generation combinations:

```use warnings;
use strict;

my \$i = 1;
print \$i++ . ". \$_\n"
for glob
"{'I','You','We'}{' play',' do not play'}{' soccer'}{' at home.',' at
+school.'}";

Output:

```1. I play soccer at home.
2. I play soccer at school.
3. I do not play soccer at home.
4. I do not play soccer at school.
5. You play soccer at home.
6. You play soccer at school.
7. You do not play soccer at home.
8. You do not play soccer at school.
9. We play soccer at home.
10. We play soccer at school.
11. We do not play soccer at home.
12. We do not play soccer at school.

Hi Kenosis,

Thank you very much for the code. Your code will help me solve a problem in computational genomics.

With deep regards,

Re^3: How can one get all possible combinations of a string without changing positions & using window size?
by ig (Vicar) on Apr 21, 2013 at 06:34 UTC

I am not certain about your objectives, but maybe something like the following would do what you want. It doesn't use a window but it does add numbers, as I understand your description.

```use strict;
use warnings;

my \$string = "ATATGCGCAT";

my %levels = (
A   => 3,
T   => 2,
G   => 2,
C   => 1,
);

my @letters = split(//,\$string);
my \$max = \$#letters;

my @numbers = map { 1 } @letters;

while(1) {
# Print the current combination of letters and numbers
print "~";
for my \$n (0..\$max) {
print \$letters[\$n] . \$numbers[\$n];
}
print "~\n";

# Calculate the next set of numbers
my \$n = 0;
\$numbers[\$n]++;
while(\$numbers[\$n] > \$levels{\$letters[\$n]}) {
\$numbers[\$n] = 1;
\$n++;
last if(\$n > \$max);
\$numbers[\$n]++;
}
last if(\$n > \$max);
}

Hi ig,

Thank you very much for the code. You have written it very nicely. Moreover, it is intelligible to me. I am grateful to you for your endeavour. I hope this code will solve some basic problems in comutational genomics.

With regards,

Create A New User
Node Status?
node history
Node Type: note [id://1029708]
help
Chatterbox?
 Discipulus is managing to update/crack his first garmin navigtor: until now just paper maps Discipulus and discoverd it use sqlite db.. DBI can read it!

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2017-09-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (280 votes). Check out past polls.

Notices?