forked from AdaGold/integer-palindrome-check
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathinteger_palindrome_check.rb
41 lines (35 loc) · 1.07 KB
/
integer_palindrome_check.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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)
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
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