From 142b744b3055f5283ccbead86377becbcc4b4cd7 Mon Sep 17 00:00:00 2001 From: Valerie Gidzinski Date: Fri, 5 Oct 2018 21:03:05 -0700 Subject: [PATCH 1/2] Passes all tests --- lib/integer_palindrome_check.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/integer_palindrome_check.rb b/lib/integer_palindrome_check.rb index dd4deea..1f08a0f 100644 --- a/lib/integer_palindrome_check.rb +++ b/lib/integer_palindrome_check.rb @@ -1,4 +1,15 @@ # Returns true if the input positive integer number forms a palindrome. Returns false otherwise. def is_palindrome(number) - raise NotImplementedError + return false if number.nil? + + original = number + reverse = 0 + + while number > 0 + last_digit = number % 10 + reverse = reverse * 10 + last_digit + number = number / 10 + end + + return original == reverse end From 331887a3d79988828a4ed9422b5e7027139873bb Mon Sep 17 00:00:00 2001 From: Valerie Gidzinski Date: Fri, 5 Oct 2018 21:45:51 -0700 Subject: [PATCH 2/2] Add timeand space complexity comments --- lib/integer_palindrome_check.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/integer_palindrome_check.rb b/lib/integer_palindrome_check.rb index 1f08a0f..21f7235 100644 --- a/lib/integer_palindrome_check.rb +++ b/lib/integer_palindrome_check.rb @@ -13,3 +13,8 @@ def is_palindrome(number) return original == reverse end + +# Time complexity: linear +# Given a number of n digits, the method has a time complexity of O(n). The number of digits is directly proportionate to the the number of times the loop executes. +# Space complexity: constant +# No matter the value of n, the method will require the same amount of memory.