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

Add possible solution to the "Reverse Parenthesis" problem #68

Merged
merged 3 commits into from
Jan 17, 2017

Conversation

zxul767
Copy link
Contributor

@zxul767 zxul767 commented Nov 3, 2016

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

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.
@zxul767
Copy link
Contributor Author

zxul767 commented Nov 9, 2016

@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.
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

@sergiolagunaswize sergiolagunaswize Dec 7, 2016

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)

Copy link
Contributor Author

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.

Copy link
Contributor

@jcperez jcperez Dec 10, 2016

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

Copy link
Contributor

@bobmazanec bobmazanec left a 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.

@jcperez
Copy link
Contributor

jcperez commented Jan 16, 2017

Should we merge this?

@jcperez jcperez self-requested a review January 16, 2017 21:14
@zxul767
Copy link
Contributor Author

zxul767 commented Jan 16, 2017

@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?

@bobmazanec
Copy link
Contributor

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?
Please submit a PR against https://github.com/wizeline/wize-docs/blob/master/CONTRIBUTING.md 🤓

@jcperez
Copy link
Contributor

jcperez commented Jan 16, 2017

@zxul767 Please merge then :) Thanks!

@zxul767 zxul767 merged commit 62bbf48 into master Jan 17, 2017
@jcperez jcperez deleted the reverse-parenthesis--add-posible-solution branch January 18, 2017 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants