Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer_palindrome_check - Divya #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions lib/integer_palindrome_check.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,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)
raise NotImplementedError
end

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