in reply to Merging Two Strings

Like others, I think you are going to have to clarify the rules, or pass a third parameter that indicates the initial offset. This comes close to matching your examples, but it falls down on your second. If the first character of the second string matches the last of the first (char/string respectively), then the overlap of 1 will always win.

P:\test\Vector maps>p1 perl> sub merge{ my( $s1, $s2 ) = @_; my $i=1; $i++ until substr( $s1, -$i ) eq substr( $s2, 0, $i ); return $s1 . substr( $s2, $i ); };; perl> print merge( 'ATTTA', 'TTTAA' );; ATTTAA perl> print merge( 'ATGTA', 'ATGTA' );; ATGTATGTA perl> print merge( 'ATGATG', 'ATGATG' );; ATGATGATG perl> print merge( 'ATGGTAC', 'CCGTAATG' );; ATGGTACCGTAATG

Your addendum about order still doesn't unambiguously resolve the requirement.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Merging Two Strings
by monarch (Priest) on Oct 27, 2005 at 07:14 UTC
    The most important part of programming is not the coding. It is working out what you want to achieve.

    The want is key to thinking about how.