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

lj palindrome #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion lib/palindrome_check.rb
Original file line number Diff line number Diff line change
@@ -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)

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.

Copy link
Author

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!

raise NotImplementedError
# check for nil
unless my_phrase.nil?
# remove all space from phrase
my_phrase = remove_spaces(my_phrase)
# call method to reverse the phrase
return check_pal(my_phrase)
end

return false
# raise NotImplementedError
end

def remove_spaces(phrase)
i = 0
n = phrase.length
no_space_phrase = ''
while i < n
unless phrase[i] == ' '
no_space_phrase += phrase[i]
end
i += 1
end

return no_space_phrase
end

def check_pal(phrase)
i = 0
last = phrase.length - 1
flag = true
while i < last

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.)

Copy link
Author

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.

Choose a reason for hiding this comment

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

Nice! Good thinking!

if phrase[i] != phrase[last]
flag = false

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.

Copy link
Author

Choose a reason for hiding this comment

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

Ah right, good catch!

end
i += 1
last -= 1
end
return flag
end