Created
August 18, 2012 02:57
-
-
Save jcuffe/3384070 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
def find substr, str | |
str.split('').each_with_index do |char, index| | |
if substr.chr == char | |
if substr.length == 1 or find(substr[1..-1], str[index+1..-1]) == 0 | |
return index | |
end | |
end | |
end | |
return nil | |
end | |
class FindTest < Test::Unit::TestCase | |
# should return index of first letter in matched string | |
def test_found | |
assert_equal 1, find('bcd', 'abcdefg') | |
end | |
# should return nil if the substring is not matched | |
def test_not_found | |
assert_equal nil, find('x', 'abcdefg') | |
end | |
# should accurately match from the beginning after a partial match | |
def test_consumes_partial_matches | |
assert_equal 5, find('bcd', 'abcbabcd') | |
end | |
# should return nil when attempting to match for or against the empty string | |
def test_empty_string | |
assert_equal nil, find('', 'abcdabe') | |
assert_equal nil, find('abc', '') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment