From 796bf31617688f98c0c124daf6c8fb72bcb6d75c Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 29 Oct 2018 00:28:31 -0700 Subject: [PATCH 1/2] passed all tests --- lib/palindrome_check.rb | 27 ++++++++++++++++++++++++++- specs/palindrome_check_spec.rb | 8 ++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_check.rb b/lib/palindrome_check.rb index d60efd0..c024b11 100644 --- a/lib/palindrome_check.rb +++ b/lib/palindrome_check.rb @@ -1,5 +1,30 @@ # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise. def palindrome_check(my_phrase) - raise NotImplementedError + return false if !my_phrase + return true if my_phrase == "" + return true if my_phrase.length == 1 + + min = 0 + max = my_phrase.length - 1 + check = false + + + + until min == max + unless my_phrase[min] == " " || my_phrase[max] == " " + if my_phrase[min] == my_phrase[max] + check = true + end + end + + min += 1 + + if my_phrase[max] == " " + max -= 1 + end + # require 'pry'; binding.pry + end + + return check end diff --git a/specs/palindrome_check_spec.rb b/specs/palindrome_check_spec.rb index feed08c..9fe6674 100644 --- a/specs/palindrome_check_spec.rb +++ b/specs/palindrome_check_spec.rb @@ -11,12 +11,14 @@ end it "palindrome input with even character count" do + test_string = "redder" palindrome_check(test_string).must_equal true end it "not a palindrome" do + test_string = "empty" palindrome_check(test_string).must_equal false @@ -27,6 +29,7 @@ describe "edge cases" do # if it's a string parameter, check for empty it "empty string reads the same backwards or forwards" do + test_string = "" palindrome_check(test_string).must_equal true @@ -34,30 +37,35 @@ # if the parameter is an object, check for nil it "nil object is not a string" do + test_string = nil palindrome_check(test_string).must_equal false end it "palindrome with spaces to ignore" do + test_string = "nurses run" palindrome_check(test_string).must_equal true end it "palindrome with multiple spaces to ignore" do + test_string = " pull up " palindrome_check(test_string).must_equal true end it "not a palindrome, with spaces" do + test_string = " not in " palindrome_check(test_string).must_equal false end it "single character is a palindrome" do + test_string = "A" palindrome_check(test_string).must_equal true From f99fa164db8c306e0b138966d9e82801d8ec4274 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 30 Oct 2018 17:45:04 -0700 Subject: [PATCH 2/2] refactored code for a better solution --- lib/palindrome_check.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/palindrome_check.rb b/lib/palindrome_check.rb index c024b11..17ae566 100644 --- a/lib/palindrome_check.rb +++ b/lib/palindrome_check.rb @@ -9,20 +9,33 @@ def palindrome_check(my_phrase) max = my_phrase.length - 1 check = false +# first check if string is empty or not +# if not empty then compare the pair +# if at least one of them is empty then move onto next index until it is not empty +# then compare +# if match continue moving up the index until min > max +# if at least one pair that does not match then return false for good - until min == max - unless my_phrase[min] == " " || my_phrase[max] == " " - if my_phrase[min] == my_phrase[max] - check = true - end + until min > max + # unless my_phrase[min] == " " || my_phrase[max] == " " + until my_phrase[min] != " " + min += 1 end - min += 1 - - if my_phrase[max] == " " + until my_phrase[max] != " " max -= 1 end + + if my_phrase[min] == my_phrase[max] + check = true + else + return false + end + # end + + min += 1 + max -= 1 # require 'pry'; binding.pry end