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

palindrome finished #33

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
69 changes: 68 additions & 1 deletion lib/palindrome_check.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
# A method to check if the input string is a palindrome.
# Return true if the string is a palindrome. Return false otherwise.

###all spaces must be ignored!!!

def palindrome_check(my_phrase)
raise NotImplementedError
# raise NotImplementedError
return false if my_phrase == nil
return true if my_phrase.length == 1

i = 0
j = my_phrase.length - 1

# puts palindrome_check("han nah ")

while i < j do
if my_phrase[i] == my_phrase[j]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend taking care of preceding and trailing white spaces before comparing the two characters. That way when you do compare the character at index i with the character at index j, you can be sure that they are not equal.

i += 1

until my_phrase[i] != " " do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also continue to check that i < j before comparing the character at index i with a white space.

i += 1
end

j -= 1

until my_phrase[j] != " " do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also continue to check that i < j before comparing the character at index j with a white space.

j -= 1
end

else
return false
end

end
return true

# --> check to see if my_phrase[i] is equal to my-phrase[j] x
# --> return false when no match x
# --> if [i] or [j] is a space,
# increment
# --> if i is greater than j, return true x
# check = ""
#
# puts check += my_phrase
#
# return check == string_reverse(my_phrase) ? true : false
end
#
# ##string reverse helper method
# def string_reverse(my_string)
# # my_string << "not implemented"
# return nil if my_string == nil
# return "" if my_string == ""
#
# i = my_string.length - 1
# j = 0
#
# while j < i do
# temp = my_string[i]
# my_string[i] = my_string[j]
# my_string[j] = temp
# i -= 1
# j += 1
# end
# return my_string
# end



puts palindrome_check(" pull up ") #=> true
puts palindrome_check("hannah")
puts palindrome_check("leanne")