Finding Disjoint Motifs in a Gene

Author: grondilu

http://rosalind.info/problems/itwv/

Sample input

GACCACGGTT
ACAG
GT
CCG

Sample output

0 0 1
0 1 0
1 0 0

Source code: itwv-grondilu.pl

use v6;

# THIS IS WAY TOO SLOW!!
#
my $dna = 'GACCACGGTT';
my @motif = <ACAG GT CCG>;

sub interwove($a, $b) {
    gather if none($a, $b) eq '' {
        for &?ROUTINE($a.substr(1), $b) {
            take $a.substr(0,1) ~ $_
        }
        for &?ROUTINE($a, $b.substr(1)) {
            take $b.substr(0,1) ~ $_
        }
    }
    elsif $a eq '' { take $b }
    else { take $a }
}

my %seen;
for @motif -> $a {
    my @arr = gather for @motif -> $b {
        my @interwove = interwove($a, $b).unique;
        take %seen{sort($a, $b).join(':')} //=
        +so grep rx/ <@interwove> /, $dna;
    }
    say "{@arr}"
}