From e993b5b7b9577fa37db38831d1640e803e0b4baa Mon Sep 17 00:00:00 2001 From: Arsalan Ahmed Date: Mon, 25 May 2015 19:26:56 -0400 Subject: [PATCH 1/2] implemented parser using the facets gem --- Gemfile | 1 + Gemfile.lock | 30 ------------------------------ parser.rb | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 31 insertions(+), 36 deletions(-) delete mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile index 447683a..0b9c607 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,4 @@ source 'http://rubygems.org' gem 'rspec' gem 'pry' +gem 'facets' diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index b5cb288..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,30 +0,0 @@ -GEM - remote: http://rubygems.org/ - specs: - coderay (1.1.0) - diff-lcs (1.2.5) - method_source (0.8.2) - pry (0.10.0) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.4) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.2) - rspec-support (~> 3.1.0) - rspec-support (3.1.1) - slop (3.5.0) - -PLATFORMS - ruby - -DEPENDENCIES - pry - rspec diff --git a/parser.rb b/parser.rb index 9396117..1de7072 100644 --- a/parser.rb +++ b/parser.rb @@ -1,13 +1,37 @@ require 'pry' +require 'facets' def kinda_like?(fuzzy_word, exact_word) - # implement with your code here - # breaking the method up into pieces is encouraged + if end_letters_not_jumbled(fuzzy_word, exact_word) && + (is_exact_match(fuzzy_word, exact_word) || + at_most_one_letter_different(fuzzy_word, exact_word) ) + + + true + else + false + end end -#def another_private_method -#end +def is_exact_match(fuzzy_word, exact_word) + regex = Regexp.new(fuzzy_word) + if !exact_word.match(regex).nil? + true + else + false + end +end -#def some_private_method -#end +def at_most_one_letter_different(fuzzy_word, exact_word) + extra_fuzzies = fuzzy_word.split(//).frequency - exact_word.split(//).frequency + extra_exacts = exact_word.split(//).frequency - fuzzy_word.split(//).frequency + + extra_fuzzies.size < 2 && extra_exacts.size < 2 +end + +def end_letters_not_jumbled(fuzzy_word, exact_word) + p fuzzy_word[0] != exact_word[-1] + p fuzzy_word[-1] == exact_word[0] + fuzzy_word[0] != exact_word[-1] && fuzzy_word[-1] != exact_word[0] +end From e4d03f82f9c46f4e8a919c43b4e37354aee1e094 Mon Sep 17 00:00:00 2001 From: Arsalan Ahmed Date: Mon, 25 May 2015 19:30:26 -0400 Subject: [PATCH 2/2] refactored to remove cruft --- parser.rb | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/parser.rb b/parser.rb index 1de7072..62613d6 100644 --- a/parser.rb +++ b/parser.rb @@ -2,30 +2,19 @@ require 'facets' def kinda_like?(fuzzy_word, exact_word) - if end_letters_not_jumbled(fuzzy_word, exact_word) && + end_letters_not_jumbled(fuzzy_word, exact_word) && (is_exact_match(fuzzy_word, exact_word) || at_most_one_letter_different(fuzzy_word, exact_word) ) - - - true - else - false - end end def is_exact_match(fuzzy_word, exact_word) regex = Regexp.new(fuzzy_word) - if !exact_word.match(regex).nil? - true - else - false - end + !exact_word.match(regex).nil? end def at_most_one_letter_different(fuzzy_word, exact_word) extra_fuzzies = fuzzy_word.split(//).frequency - exact_word.split(//).frequency extra_exacts = exact_word.split(//).frequency - fuzzy_word.split(//).frequency - extra_fuzzies.size < 2 && extra_exacts.size < 2 end