Dear Edward,
This was my approach to your problem. It runs through all the sequences you give it in the @sequences array looking for the one with the biggest gaps, and kicking out any where the letter order isn't right. It populates the @in_the_running array with sequences that will work. While this is happening, it makes sure that the sequence with the biggest gap stays at the front of the array (index 0). It then prints the sequence with the biggest gap, and runs through the rest of the sequences that qualified - each sequence it compares against the one with the biggest gap, letter by letter. If it matches, it prints a letter - if it doesn't it prints a dash.
This is one of those fun problems where multiple approaches could be applied - I hope mine assists you with yours!
Best,
-Adam
@in = split(//,<STDIN>); pop(@in);
$regex = '.*'.join('(.*)',@in).'.*';
@sequences = qw/ABCDHA ACDBGJ CADHMK ABICID/;
for (@sequences) {
@letters = split(//,$_);
s/.*($in[0].*$in[$#in]).*/$1/;
if ((@gaps) = (m/$regex/o)){
if (length(join('',@gaps)) > $biglen) { unshift(@in_the_running, $
+_); $biglen = length(join('',@gaps)) }
else { push(@in_the_running, $_) }
}
}
print $in_the_running[0], "\n";
@base = split(//,$in_the_running[0]);
for my $seqno (1..$#in_the_running) {
@seq = split(//,$in_the_running[$seqno]);
for $q (@base) {
if ($seq[0] eq $q) { print $q; shift(@seq); next }
else { print '-' }
}
print "\n";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.