in reply to Merging Two Strings

This seems to do what you want:

use warnings; use strict; while (<DATA>) { chomp; my $first = $_; my $second = <DATA>; last if ! defined $second; chomp $second; (print "Degenerate case: $first, $second\n"), next if length ($first) == 1 or length ($second) == 1; my $overlap = 0; while ($overlap < length $first) { my $tail = substr $first, -$overlap - 1; my $match = $tail ^ substr $second, 0, $overlap + 1; last if $overlap > 1 && $match =~ /^\0+$/; ++$overlap; } my $result = $first . substr $second, $overlap + 1; print "Merge $first and $second as $result\n"; } __DATA__ A ATG ATTTA TTTAA ATGTA ATGTA ATGATG ATGATG

Prints:

Merge ATTTA and TTTAA as ATTTAA Merge ATGTA and ATGTA as ATGTA Merge ATGATG and ATGATG as ATGATGATG
Update for sauoq's test case

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Merging Two Strings
by sauoq (Abbot) on Oct 27, 2005 at 10:47 UTC
    This seems to do what you want

    Somehow I doubt that he wants this: Merge A and ATG as AG

    -sauoq
    "My two cents aren't worth a dime.";
    

      Thanks for the test case. Any other good ones that look like failures?


      Perl is Huffman encoded by design.