-
Notifications
You must be signed in to change notification settings - Fork 45
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
i += 1 | ||
|
||
until my_phrase[i] != " " do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also continue to check that |
||
i += 1 | ||
end | ||
|
||
j -= 1 | ||
|
||
until my_phrase[j] != " " do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also continue to check that |
||
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") |
There was a problem hiding this comment.
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.