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 tests passing #24

Open
wants to merge 4 commits 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
30 changes: 29 additions & 1 deletion lib/integer_palindrome_check.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
# Returns true if the input positive integer number forms a palindrome. Returns false otherwise.
require 'pry'
def is_palindrome(number)
raise NotImplementedError
return false if number == nil || number < 0
return true if number == 0

reversed_num = 0 # n
duplicate = number # n

while duplicate > 0
reversed_num *= 10
last_num = duplicate % 10 # only contains 1 digit at a time
reversed_num += last_num
duplicate -= last_num
duplicate /= 10
end

if number == reversed_num
return true
else
return false
end
end

#Time complexity: O(n) where n is the length/size of the input.
#Depending on how many digits the number is the while loop will
# continue to execute O(n - 1), but we drop the 1 because it is insignificant.

#Space complexity: O(n) where n is the length/size of the input.
# In this solution I defined 3 variables, 2 of which take
#up the same space as the input's length. The last_num variable only
#holds one digit at a time
2 changes: 1 addition & 1 deletion specs/integer_palindrome_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
it "nil object is not an integer" do
is_palindrome(nil).must_equal false
end

it "single digit palindrome" do
is_palindrome(9).must_equal true
end
Expand Down