From b4802d6236ebf397f79e5563414f13c512b03469 Mon Sep 17 00:00:00 2001 From: Divya Date: Wed, 26 Sep 2018 14:32:05 -0700 Subject: [PATCH 1/2] Integer_palindrome_check - Divya --- lib/integer_palindrome_check.rb | 38 +++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/integer_palindrome_check.rb b/lib/integer_palindrome_check.rb index dd4deea..62cd81f 100644 --- a/lib/integer_palindrome_check.rb +++ b/lib/integer_palindrome_check.rb @@ -1,4 +1,38 @@ # Returns true if the input positive integer number forms a palindrome. Returns false otherwise. def is_palindrome(number) - raise NotImplementedError -end + # A method to check if the input string is a palindrome. + # Return true if the string is a palindrome. Return false otherwise. + if number == nil || number < 0 + return false + end + + length = 0 + counter = number + + while counter >= 10 + counter /= 10 + length += 1 + end + + index = 0 + first_index = 0 + last_index = length - 1 + check = true + + check = true + while index < length + + # palindrome check + first_digit = number / (10 ** (length-(index))) + # removes first digit + number = number - (first_digit * (10 ** (length-index))) + last_digit = number % (10) + # removes last digit + number = number / 10 + if first_digit != last_digit + check = false + end + index += 2 + end + return check + end From de3dc78241ac76747b22e1730ebf567cb51bcc38 Mon Sep 17 00:00:00 2001 From: Divya Date: Wed, 26 Sep 2018 22:15:44 -0700 Subject: [PATCH 2/2] Added time and space complexity to file --- lib/integer_palindrome_check.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/integer_palindrome_check.rb b/lib/integer_palindrome_check.rb index 62cd81f..adef5c6 100644 --- a/lib/integer_palindrome_check.rb +++ b/lib/integer_palindrome_check.rb @@ -1,7 +1,11 @@ # Returns true if the input positive integer number forms a palindrome. Returns false otherwise. +# A method to check if the input string is a palindrome. +# Return true if the string is a palindrome. Return false otherwise. +# Time complexity - O(n/2) --> O(n) +# Space complexity is a CONSTANT as the pointer for the variable storing the +# number is changing but not extra space is occupied by the changing values. def is_palindrome(number) - # A method to check if the input string is a palindrome. - # Return true if the string is a palindrome. Return false otherwise. + if number == nil || number < 0 return false end @@ -17,7 +21,6 @@ def is_palindrome(number) index = 0 first_index = 0 last_index = length - 1 - check = true check = true while index < length