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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merging Two Strings
by monarch (Priest) on Oct 27, 2005 at 07:14 UTC |