From 304c9b1e85d0ec9e793788e2d139e1190ead1145 Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Thu, 27 Sep 2018 09:21:36 -0700 Subject: [PATCH] integer_palindrome --- lib/integer_palindrome_check.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/integer_palindrome_check.rb b/lib/integer_palindrome_check.rb index dd4deea..2b5bf9a 100644 --- a/lib/integer_palindrome_check.rb +++ b/lib/integer_palindrome_check.rb @@ -1,4 +1,24 @@ # Returns true if the input positive integer number forms a palindrome. Returns false otherwise. def is_palindrome(number) - raise NotImplementedError + if number.nil? || number < 0 + return false + end + + if number / 10 < 1 + return true + end + + l = number.to_s.length + i = 0 + + while i < l / 2 + + if number / ( 10 ** (l - i - 1)) % 10 != number % ( 10 ** (i + 1)) / (10 ** i) + return false + end + + i += 1 + end + return true + end