From 53f16ea5f040c744d301c658094b9573edff8f52 Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sat, 19 Sep 2015 16:52:05 -0400 Subject: [PATCH] write fuzzy parser --- parser.rb | 57 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/parser.rb b/parser.rb index 9396117..072b68e 100644 --- a/parser.rb +++ b/parser.rb @@ -1,13 +1,56 @@ require 'pry' -def kinda_like?(fuzzy_word, exact_word) - # implement with your code here - # breaking the method up into pieces is encouraged +# 'f' for fuzzy, 'e' for exact word +def kinda_like?(f,e) + # exactMatch(f,e) + f == e || middleJumbled?(f,e) || offByOne?(f,e) end -#def another_private_method -#end +# check if you can turn the exact word into the fuzzy one by jumbling up all +# of the exact word's letters, while keeping the first and last as is +def middleJumbled?(f,e) + len = e.length + if len >= 2 + # turn "coffee" into /c[coffee]{3,}e/, for instance + # the {3,} prohibits cutting out a big chunk of the middle of the word + regex = Regexp.new "#{e[0]}[#{e}]{#{len-3},}#{e[len-1]}" + regex === f + else + false + end +end + +# checks whether a single substition, insertion, or deletion will turn the +# exact word into the fuzzy word +def offByOne?(f,e) + if f.length == e.length + return singleSubstition?(f,e) + else + return singleInsertion?(f,e) + end +end -#def some_private_method -#end +# checks if a single letter-for-letter solution will do the trick +def singleSubstition?(f,e) + differences = 0 + for i in 0...f.length + if f[i] != e[i] then differences += 1 end + if differences > 1 then return false end + end + return true +end + +# checks if a single insertion or deletion will do the trick +def singleInsertion?(f,e) + longer = [f,e].max + shorter = [f,e].min + offset = 0 + index = 0 + # wherever the two words differ, increase the offset and try again + while index < shorter.length do + shorter[index] == longer[index+offset] ? index += 1 : offset += 1 + if offset > 1 then return false end + end + return true +end