-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add possible solution to the "Reverse Parenthesis" problem #68
Conversation
I added this solution because I noticed the observations mentioned that you needed a stack and a queue to solve it. You don't really need a queue, as the proposed solution shows. I also added a few extra test cases that test extreme cases, which is where most candidates typically trip up. I think they can give us a good chance to test their debugging skills after they've implemented a partially correct solution.
We're not modifying the `i` index, so this makes the code a bit simpler.
@oryws @bobmazanec Adding some additional test cases is important, since there are some solutions that will pass simple tests but will definitely fail for more elaborate ones. For example, I just interviewed a candidate who wrote a rather concise, "partially correct" solution: def revert_words_in_parenthesis(word):
opening = []
closing = []
for idc, char in enumerate(word):
if char == '(':
opening.append(idc)
if char == ')':
closing.append(idc)
for s, e in zip(reversed(opening), closing):
word[s + 1: e] = reversed(word[s + 1: e])
return ''.join(char for char in word if char != '(' and char != ')')
def test():
examples = ['', '()' ,'(())(((())))', '(bar)', '((bar))', 'wi(ez)(((il)))(en)']
expected = ['', '', '', 'rab', 'bar', 'wizeline']
results = [revert_words_in_parenthesis(list(word)) for word in examples]
for i in range(len(results)):
print(results[i] == expected[i], results[i], expected[i])
examples = ['foobarbaz', 'foo(bar)baz', 'foo(bar(baz))blim', '(tr(e)e)']
results = [revert_words_in_parenthesis(list(word)) for word in examples]
expected = ['foobarbaz', 'foorabbaz', 'foobazrabblim', 'eert']
for i in range(len(results)):
print(results[i] == expected[i], results[i], expected[i]) with the following output: (True, '', '')
(True, '', '')
(True, '', '')
(True, 'rab', 'rab')
(True, 'bar', 'bar')
(False, 'wineilze', 'wizeline')
(True, 'foobarbaz', 'foobarbaz')
(True, 'foorabbaz', 'foorabbaz')
(True, 'foobazrabblim', 'foobazrabblim')
(True, 'eert', 'eert') |
@@ -13,13 +13,45 @@ Write a method that for a given input string, reverse all the characters inside | |||
``` | |||
|
|||
## Observations | |||
- How to solve it? Using a stack and a queue. | |||
- How to solve it? Either recursion or using a stack. |
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.
@zxul767 detecting a malformed expression using a stack is clear for me, but could you please elaborate on how would you detect one using recursion? I like this problem :p
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.
One way to detect this in a recursive implementation would be to return from the recursive call when you find a closing parenthesis (or when you reach the end of the string) but without consuming the token, and then check --in the code the made the recursive call-- that you have a closing parenthesis. If you don't have a closing parenthesis, then you ran into a malformed expression.
Let me know if this makes sense
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 like this to say something like: Two possible solutions for this problem are using recursion or using a stack
, so we say that we are not married with only two approaches to this problem (I thought in one solution using sort, and I am not doubting there are even more)
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 agree, I'll update the statement to that.
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 added that problem to the original set of questions, but Bob added to this Github repo.
The solution says a queue because you need some kind of buffer to keep the current string. This problem is from the OMI contest back in 2004. Contestants can only use C or Pascal, that's why you cannot do something like reversed(word[s + 1: e])
in a "easy" way.
But... I'm ok with removing it from the text, just want to give more context :)
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 test cases! I added them to the problem in our HackerRank library.
Should we merge this? |
@jcperez I think we can... @bobmazanec has set as a guideline that if there are no further comments after a while, it's probably time to go on and merge. Who's supposed to merge though? The original submitter or the reviewers? |
I suggest the submitter — presumably they are "eager" for it. Have arguments for/against and other another idea? |
@zxul767 Please merge then :) Thanks! |
I added this solution because I noticed the observations mentioned that you needed a stack and a queue to solve it. You don't really need a queue, as the proposed solution shows.
I also added a few extra test cases that test extreme cases, which is where most candidates typically trip up. I think they can give us a good chance to test their debugging skills after they've implemented a partially correct solution.
Please have a look and let me know your feedback (@oryws I'm adding you as a reviewer since you've recently interviewed someone using this problem)
Thanks