From: Daniel N Date: 2007-03-28T10:13:07+09:00 Subject: Re: String Intersection. A better way ------=_Part_5152_16117607.1175044385171 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 3/28/07, Rob Biedenharn wrote: > > On Mar 27, 2007, at 8:13 PM, Timothy Hunter wrote: > > Daniel N wrote: > >> Hi all, > >> > >> I have an issue with strings. I want the intesection of two > >> strings but I'm > >> not sure how to go about it. I have two cases. > >> > >> Case 1: > >> String 1 has the tail end intersecting with the start of string > >> 2.. eg: > >> > >> String 1: The quick brown fox jumped over the lazy dog. > >> String 2: fox jumped over the lazy dog. And in other news bla bla > >> bla > >> > >> The problem is I don't know how much is intersecting. > >> What I've done is to take string 1 and say psudo code > >> > >> until match found > >> string2 =~ /^(value of string1)/ > >> if match found set flag > >> else delete first char of string1 > >> loop again > >> > >> What is a better way to do this? > >> > >> Case 2: > >> > >> The start of String1 intersects with the end of string2 eg: > >> > >> String1: And here we are with another string. > >> String2: bla bla bla. And here we are > >> > >> I don't really know about this one yet... I haven't gotten to it. > >> > >> Can anyone pls suggest what I might do? > >> > >> Thanx > >> Daniel > >> > > Take a look at the MatchData class, particularly the #begin and > > #offset methods. > > > > What you might do is take a TDD approach (Test Driven Development)... > > Change the method body until the tests pass and you're all set! Of > course, seeing the tests might help you solidify what you expect the > behavior to be. > > class String > # intersection of self and other > def &(other) > '' > end > end > > if __FILE__ == $0 > require 'test/unit' > class StringIntersectionTest < Test::Unit::TestCase > def setup > @empty = '' > end > > def test_empty_string > assert_equal @empty, @empty & @empty, "empty with empty" > assert_equal @empty, 'not' & @empty, "non-empty with empty" > assert_equal @empty, @empty & 'not', "empty with non-empty" > end > > def test_equal_strings > s = "Some long string that I'm ready to take to the intersection" > assert_equal s, s & s, "string with self" > end > > def test_prefix > assert_equal 'f', 'f' & 'fr', 'single char string as prefix of > other' > assert_equal 'f', 'fr' & 'f', 'other is single char prefix of > string' > assert_equal 'fred', 'fred' & 'fredrick', 'multi-char string > as suffix of other' > assert_equal 'fred', 'fredrick' & 'fred', 'other is multi-char > suffix of string' > end > > def test_suffix > assert_equal 'r', 'r' & 'fr', 'single char string as suffix of > other' > assert_equal 'r', 'fr' & 'r', 'other is single char suffix of > string' > assert_equal 'rick', 'rick' & 'fredrick', 'multi-char string > as suffix of other' > assert_equal 'rick', 'fredrick' & 'rick', 'other is multi-char > suffix of string' > end > > def test_other_intersections > assert_equal 'site', 'new web site' & 'site-wide policy' > end > > def test_other_empty_intersections > assert_equal @empty, 'fred' & 'ethel' > assert_equal @empty, 'The quick brown fox jumps over the lazy > dog.' & 'The lazy dog lies under the jumping fox.' > assert_equal @empty, 'Blah, Blah, blah' & 'yada, yada, yada' > end > end > end Whoa... Thanx for the tests Rob. ------=_Part_5152_16117607.1175044385171--