I haven't tested the code below very much; you should if you think you many want to use it.

#!/usr/bin/perl -wl use strict; use List::Util qw( min max sum ); sub score { my ($str, @array) = @_; my @pos = sort { $a->[ 0 ] <=> $b->[ 0 ] } map [ $_->[ 0 ], $_->[ 0 ] + length $_->[ 1 ] ], grep $_->[ 0 ] > -1, map [ index( $str, $_ ), $_ ], @array; for my $i ( reverse 0 .. $#pos - 1 ) { my $lefts = [ sort { $a <=> $b } map $_->[ 0 ], @pos[ $i, $i + 1 +] ]; my $rights = [ sort { $b <=> $a } map $_->[ 1 ], @pos[ $i, $i + 1 +] ]; if ( $rights->[ 1 ] - $lefts->[ 1 ] > 0 ) { splice @pos, $i, 2, [ map $_->[ 0 ], $lefts, $rights ] } } return sum map $_->[ 1 ] - $_->[ 0 ], @pos; } my $s1 = 'GATTACGAGTGGCGCTCGTGTAACGGCA'; my @ar = ('GATTACG','GCGCTCG','AACGGCA'); #21 (0,11,21) my @ar2 = ('GATTACG','TTACGAG','CGTGTAA'); #16 my @ar3 = ('TACGAGT','GTGGCGC','GCTCGTG'); #17 print score( $s1, @$_ ) for \@ar, \@ar2, \@ar3; __END__ % perl score.pl 21 16 17

the lowliest monk


In reply to Re: Substring Distance Problem by tlm
in thread Measuring Substrings Problem by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.