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

Fixes issue #3263-Code implementation in C++ for balanced_expression in stack data structure #3624

Closed
wants to merge 10 commits into from

Conversation

bahl24
Copy link

@bahl24 bahl24 commented Jun 29, 2018

Fixes issue:

Fixes issue #3263-Code implementation in C++ for balanced_expression in stack data structure.

Changes:

A C++ implementation for balanced_expression was added. This expands the language base for the Stack data structure for which currently only Java implementation is available.

@bahl24
Copy link
Author

bahl24 commented Jun 29, 2018

The code takes a string of bracket as input from stdin and prints YES if the string is balanced and NO if string is not balanced.
Input format-

  1. First line takes in number of test cases as t.
  2. Next t lines takes in the strings to be tested.
    As I am a newbie, please give a review. @AdiChat @arnavb

Copy link
Member

@arnavb arnavb left a comment

Choose a reason for hiding this comment

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

Please add spaces directly after keywords like if and for.

#include <cstring>
#include <stack>

using namespace std;
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid this.


bool isOpen(char c)
{
if(c == '(' || c == '{' || c == '[')
Copy link
Member

Choose a reason for hiding this comment

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

How about: return c == '(' || c == '{' || c == '[';? You could also consider marking this function inline.

Copy link
Author

Choose a reason for hiding this comment

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

As the return type is bool so we need to return true or false and not 1 or 0. I have checked that the code fails in that case.


bool isBalanced(char popped, char now)
{
if(popped == '(' && now ==')')
Copy link
Member

Choose a reason for hiding this comment

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

return (popped == '(' && now ==')') ||
    (popped == '[' && now ==']') ||
    (popped == '{' && now =='}');

Copy link
Author

Choose a reason for hiding this comment

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

As the return type is bool so we need to return true or false and not 1 or 0. I have checked that the code fails in that case.

Copy link
Member

Choose a reason for hiding this comment

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

@bahl24 Sorry what? Last I checked, the or operator returns a bool value. 1 and 0 evaluate to true and false respectively anyway. This shouldn't be a problem, unless the return type was that of an integer (it isn't). Could you clarify what test cases this fails in?

Copy link
Author

@bahl24 bahl24 Jul 2, 2018

Choose a reason for hiding this comment

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

Sample input- (})
It fails to output "NO" when we use return (popped == '(' && now ==')') || (popped == '[' && now ==']') || (popped == '{' && now =='}');
But, it shouldn't be a problem as the latest code passes any sample input given and is more easy to understand.
@arnavb @AdiChat

Copy link
Member

@arnavb arnavb Jul 2, 2018

Choose a reason for hiding this comment

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

Actually, it does output NO. Check this and modify as you desire (write #define ARNAVB_VERSION at the beginning of the file to check my version): https://wandbox.org/permlink/ygACUgdFsVMLdG8Q

As for your second point, I personally disagree, since it is repetitive, but it is a matter of opinion so I'm not going to contradict you there. However, the code I gave definitely works, as both snippets are logically equivalent, as shown in the wandbox above (I used the sample input you gave).

{
/* The code takes a string str as input from stdin and prints "YES"
if str is a balanced expression else it prints "NO".*/
stack<char> s;
Copy link
Member

Choose a reason for hiding this comment

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

Fix your indentation here please.

if str is a balanced expression else it prints "NO".*/
stack<char> s;
string str;
cin>>str;
Copy link
Member

Choose a reason for hiding this comment

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

Add spaces around operators please.

string str;
cin>>str;
int no = 0;
for(int i = 0; i < str.length(); i++)
Copy link
Member

Choose a reason for hiding this comment

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

I think a range based for loop might be clearer, unless i is used somewhere in the loop.

Copy link
Author

Choose a reason for hiding this comment

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

As i is being used inside the loop, a range based loop will lead in more execution time or might be insufficient if str is quite long. Plus using str.length() makes viewer exactly understand the code.

}
}
}
if(s.size() != 0)
Copy link
Member

Choose a reason for hiding this comment

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

if (!s.empty())

@bahl24
Copy link
Author

bahl24 commented Jun 30, 2018

@arnavb I have reviewed the changes suggested by you and have made the changes wherever applicable. I have also justified my reasons few changes suggested.

@bahl24
Copy link
Author

bahl24 commented Jul 3, 2018

@arnavb @AdiChat I have incorporated all the changes suggested by @arnavb . Sorry for the trouble, but now the code is working correctly.

#include <iostream>
#include <stack>

using namespace std;
Copy link
Member

Choose a reason for hiding this comment

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

Just remove usage of using namespace std, and you're good to go!

Copy link
Author

Choose a reason for hiding this comment

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

@bahl24
Copy link
Author

bahl24 commented Jul 7, 2018

@arnavb @AdiChat any problem with the code?

@bahl24 bahl24 closed this Oct 15, 2018
@bahl24 bahl24 reopened this Oct 15, 2018
@bahl24 bahl24 closed this Oct 15, 2018
@bahl24 bahl24 mentioned this pull request Oct 15, 2018
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.

None yet

2 participants