String Intersection. A better way

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

Daniel N wrote:

String 2: fox jumped over the lazy dog. And in other news bla bla bla
What is a better way to do this?
Can anyone pls suggest what I might do?

Thanx
Daniel

Take a look at the MatchData class, particularly the #begin and #offset
methods.

On 3/28/07, Timothy H. [email protected] wrote:

loop again
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.

Thanx I didn’t know about that one. But I still don’t know how that
will
help me. Could you break it down for me pls?

Thanx

On 3/28/07, Rob B. [email protected] wrote:

String 1 has the tail end intersecting with the start of string
string2 =~ /^(value of string1)/
String1: And here we are with another string.
#offset methods.
def &(other)

end
end

Whoa… Thanx for the tests Rob.

On Mar 27, 2007, at 8:13 PM, Timothy H. wrote:

else delete first char of string1

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

Note that two of the six tests already pass just by returning the
empty string :wink:

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Daniel N schrieb:

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.
(…)

Daniel, if your strings aren’t too big, you could try this:

class String
def intersection_at_start(other)
not empty? and other[/#{split(//)"("}#{")?"(size-1)}\z/]
end
def intersection_at_end(other)
other.intersection_at_start(self)
end
def &(other)
intersection_at_start(other) or intersection_at_end(other) or “”
end
end

I don’t know if this is faster than your original implementation, and it
certainly isn’t easier to understand, but at least it passes Rob’s
tests.

Regards,
Pit