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

all tests passed, int palindrome = done. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Integer Palindrome Check
Check if the digits in a non-negtive integer form a palindrome.
Check if the digits in a non-negative integer form a palindrome.

## Exercise
* Design and implement a method that checks if the digits in the input positive integer forms a palindrome. The method returns true if the digits in the input form a palindrome. The method returns false otherwise.
Expand Down
39 changes: 38 additions & 1 deletion 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.
def is_palindrome(number)
raise NotImplementedError
# raise NotImplementedError
return false if number == nil || number < 0
return true if number < 10 || number == 0

i = number
num = []
num_reverse = []

while i > 0 #while it is a pos num
digit = i % 10 #digit is the number modulo 10
i /= 10 #divide i by 10 to remove or 'pop' last digit
num_reverse << digit #push digit to reverse array
num << digit #shovel digit to num array (to be reversed below)
end

num = array_reverse(num)

return num == num_reverse ? true : false

end

#helper method for reversing array
def array_reverse(array)
i = array.length - 1
j = 0

while j < i do
temp = array[i]
array[i] = array[j]
array[j] = temp
i -= 1
j += 1
end
return array
end

is_palindrome(1001)

# [[log-base10(100) = 2] = [10 * 10 = 100]]