From b40fe95c437a08ba908cfcd9581204eddcde751f Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 2 Jul 2021 12:45:20 -0700 Subject: [PATCH] Extract split method from loop --- .../arrays/strings/almost_palindrome_checker.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/strings/almost_palindrome_checker.rb b/data_structures/arrays/strings/almost_palindrome_checker.rb index 67c644e..75e2212 100644 --- a/data_structures/arrays/strings/almost_palindrome_checker.rb +++ b/data_structures/arrays/strings/almost_palindrome_checker.rb @@ -25,10 +25,11 @@ def almost_palindrome_checker(string) p1 = 0 p2 = string.length - 1 + array = string.split('') while p1 < p2 - if string.split('')[p1] != string.split('')[p2] - return palindrome_checker(string, p1, p2 - 1) || palindrome_checker(string, p1 + 1, p2) + if array[p1] != array[p2] + return palindrome_checker(array, p1, p2 - 1) || palindrome_checker(array, p1 + 1, p2) end p1 += 1 p2 -= 1 @@ -37,9 +38,9 @@ def almost_palindrome_checker(string) true end -def palindrome_checker(string, p1, p2) +def palindrome_checker(array, p1, p2) while p1 < p2 - if string.split('')[p1] != string.split('')[p2] + if array[p1] != array[p2] return false end p1 += 1