Last active
December 7, 2016 19:13
-
-
Save holyketzer/fc37366ee4afdd61b38e36496d662193 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
def single_char_edit?(a, b) | |
if a.size == b.size | |
diffs = 0 | |
a.size.times do |i| | |
if a[i] != b[i] | |
diffs += 1 | |
end | |
end | |
diffs == 1 | |
elsif (a.size - b.size).abs == 1 | |
long = a | |
short = b | |
if b.size > a.size | |
long = b | |
short = a | |
end | |
diffs = 0 | |
short.size.times do |i| | |
if short[i] != long[i + diffs] | |
diffs += 1 | |
if diffs > 1 | |
break | |
end | |
end | |
end | |
if diffs.zero? | |
diffs = 1 # for the length difference | |
end | |
diffs == 1 | |
else | |
false | |
end | |
end | |
def check(a, b, expected) | |
if single_char_edit?(a, b) == expected | |
puts "OK #{a.inspect}, #{b.inspect} = #{expected}" | |
true | |
else | |
puts "FAIL #{a.inspect}, #{b.inspect}, expected to eq #{expected}" | |
false | |
end | |
end | |
def test_suite | |
res = [ | |
check("abc", "ab", true), | |
check("abc", "abe", true), | |
check("abc", "abc", false), | |
check("abc", "bb", false), | |
check("abc", "ABc", false), | |
check("123", "32", false), | |
check("1234", "134", true), | |
] | |
if res.all? | |
puts '... Complete sucess! ...' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment