good chemistry is complicated,and a little bit messy -LW PerlMonks

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

by hdb (Prior)
 on Apr 20, 2013 at 20:09 UTC ( #1029683=note: print w/replies, xml ) Need Help??

I am not sure I understand the question. Can it be reformulated as: get all 2^10 combinations of 1s and 2s and insert them after each letter in the given string? For this question the answer would be:

```use strict;
use warnings;
my \$string = "ATATGCGCAT";
my @letters = split '', \$string;
for( my \$i=0; \$i<2**@letters; ++\$i ) {
my \$b = sprintf("%010b",\$i);
\$b=~tr/01/12/;
for( my \$j=0; \$j<@letters; ++\$j ) {
print \$letters[\$j],substr \$b, \$j, 1;
}
print "\n";
last if \$i==20; # remove if you want all...
}

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

Replies are listed 'Best First'.
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

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.

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,

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,

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 01:22 UTC

Hi hdb

Thanks for your quick reply and the code. I am not sure whether it can be reformulated as 2^10 combinations of 1's and 2's. But your code has partly solved my problem because it has produced some combinations without changing positions of letters in the string. I shall try to understand your code. If I find any difficulty, I shall get back to you.

With regards,

Create A New User
Node Status?
node history
Node Type: note [id://1029683]
help
Chatterbox?
 [stevieb]: very interesting XS question over on SO

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-08-20 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (317 votes). Check out past polls.

Notices?