-
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
lj palindrome #37
base: master
Are you sure you want to change the base?
lj palindrome #37
Conversation
lib/palindrome_check.rb
Outdated
flag = true | ||
while i < last | ||
if phrase[i] != phrase[last] | ||
flag = false |
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.
Instead of setting the flag as false, you could consider stopping the loop and returning false here. If the first check itself does not match, you don't need to check the rest to conclude that it is not a palindrome.
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.
Ah right, good catch!
lib/palindrome_check.rb
Outdated
i = 0 | ||
last = phrase.length - 1 | ||
flag = true | ||
while i < last |
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.
Can you think of a way to account for skipping consecutive white spaces in this loop? (Instead of altering the input string parameter by removing spaces in it.)
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.
Without altering the input parameter, I suppose I could check for spaces in the comparison method. So if phrase[i] == ' ' then increment i before comparing or if phrase[last] == ' ' then decrement last before comparing. In this case I'm guessing this would reduce my space complexity and potentially my time complexity. So no additional string would have to be stored and while iterating across the array in for comparison of i (start) and last (end) characters the space ( ' ') occurrence could be accounted for as they came up instead of necessitating that the entire string is iterated across first. For example in my implementation a string with no spaces will get iterated across to check for spaces and a new string gets stored even, if its an exact copy of the original. Taking your modified approach would definitely eliminate additional memory use and redundant iteration of the entire string.
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.
Nice! Good thinking!
@@ -1,5 +1,44 @@ | |||
# A method to check if the input string is a palindrome. | |||
# Return true if the string is a palindrome. Return false otherwise. | |||
def palindrome_check(my_phrase) |
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 don't see any comments on the time and space complexity. Can you think through the time and space complexity of your algorithm?
Gentle reminder: Always explain what n stands for while explaining your time and space complexity. That along with your explanation for reasoning behind your answer, is the complete answer for the time and space complexity. In this case, n is the number of characters in the input string parameter.
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.
Thanks for the feedback Shruti this is very helpful -I pushed up a refactor on this and included my thoughts on T and S complexity following your feedback above. I'm not confident in in my answer on T complexity when while loops are nested within each other so if you have clarification on calculating those I appreciate it!
I added some comments inline. After you give those some thought, take a look at my solution here: https://github.com/Ada-C10/palindrome_check/blob/solution/lib/palindrome_check.rb |
Nice work! See comments added inline. |
# increment/decrement in unison) O(n) | ||
# Using this method no additional space is required beyond the | ||
# head and tail incrementors as all non-whitespace characters are checked in | ||
# place and only a boolean is returned O(n) |
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.
Nicely done! The time complexity is correct. The space complexity of your algorithm is O(1). See my implementation of the same algorithm on https://github.com/Ada-C10/palindrome_check/blob/solution/lib/palindrome_check.rb
No description provided.