-
-
Save mycodeschool/7207410 to your computer and use it in GitHub Desktop.
/* | |
C++ Program to check for balanced parentheses in an expression using stack. | |
Given an expression as string comprising of opening and closing characters | |
of parentheses - (), curly braces - {} and square brackets - [], we need to | |
check whether symbols are balanced or not. | |
*/ | |
#include<iostream> | |
#include<stack> | |
#include<string> | |
using namespace std; | |
// Function to check whether two characters are opening | |
// and closing of same type. | |
bool ArePair(char opening,char closing) | |
{ | |
if(opening == '(' && closing == ')') return true; | |
else if(opening == '{' && closing == '}') return true; | |
else if(opening == '[' && closing == ']') return true; | |
return false; | |
} | |
bool AreParanthesesBalanced(string exp) | |
{ | |
stack<char> S; | |
for(int i =0;i<exp.length();i++) | |
{ | |
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[') | |
S.push(exp[i]); | |
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']') | |
{ | |
if(S.empty() || !ArePair(S.top(),exp[i])) | |
return false; | |
else | |
S.pop(); | |
} | |
} | |
return S.empty() ? true:false; | |
} | |
int main() | |
{ | |
/*Code to test the function AreParanthesesBalanced*/ | |
string expression; | |
cout<<"Enter an expression: "; // input expression from STDIN/Console | |
cin>>expression; | |
if(AreParanthesesBalanced(expression)) | |
cout<<"Balanced\n"; | |
else | |
cout<<"Not Balanced\n"; | |
} |
I'm having a hard time with this. How can i make it so that instead of having the user input the string of characters it instead reads it from a file that has lines of different expressions.
return S.empty() ? true:false;
is kind of redundant (if true return true else return false).
It's not redundant
what if the input is "(".
it will fail for the test case when we include the space in the user input because the taking of string input will not execute after a blank space
`def balancedBrackets(string):
#remove all invalid characters. This is optional by the way.If your string only contains [{()}]. You don't have to change input string in any
way
string = [x for x in string if x in '[{()}]]']
stack = []
for char in string:
if char in '([{':
stack.append(char)
else:
if len(stack) == 0 or not arePair(stack[-1], char):
return False
else:
stack.pop()
if len(stack) == 0:return True
return `False`
def arePair(opening,closing):
if opening == '(' and closing == ')':return True
elif opening == '[' and closing == ']': return True
elif opening == '{' and closing == '}':return True
return False
`
Solution Using JavaScript: you can also check the whiteboard and test from here:
class MultiBracketValidation {
constructor() {
this.stack = new Stack();
this.opening = ["{", "(", "[", "<"];
this.closing = ["}", ")", "]", ">"];
}
checkForValidation(input) {
for (const char of input) {
if (this.isOpening(char)) {
this.stack.push(char);
} else if (this.isClosing(char)) {
if (
this.stack.isEmpty() ||
!this.isPair(this.stack.peek().value, char)
) {
return false;
} else {
this.stack.pop();
}
}
}
return this.stack.isEmpty();
}
isOpening(char) {
return this.opening.indexOf(char) >= 0;
}
isClosing(char) {
return this.closing.indexOf(char) >= 0;
}
isPair(opening, closing) {
return this.closing.indexOf(closing) === this.opening.indexOf(opening);
}
}
Hey, I am a beginner in programming, and tried to implement the pseudo code shared in the video using C++, can you guys share the reviews about the coding style.
bool checkBalancedParenthesis(string exp){
stack<char> s;
int n = exp.length();
for(int i=0; i<n; i++){
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '['){
s.push(exp[i]);
}
else if(exp[i] == '}' || exp[i] == ')' || exp[i] == ']'){
if (s.empty())
return false;
switch (s.top())
{
case '(':
if (exp[i] == ')')
{s.pop();
break;}
else{
return false;
}
case '{':
if (exp[i] == '}')
{s.pop();
break;}
else{
return false;
}
case '[':
if (exp[i] == ']')
{s.pop();
break;}
else{
return false;
}
}
}
}
return s.empty()?true:false;
}
Hey, I am a beginner in programming, and tried to implement the pseudo code shared in the video using C++, can you guys share the reviews about the coding style.
bool checkBalancedParenthesis(string exp){ stack<char> s; int n = exp.length(); for(int i=0; i<n; i++){ if (exp[i] == '{' || exp[i] == '(' || exp[i] == '['){ s.push(exp[i]); } else if(exp[i] == '}' || exp[i] == ')' || exp[i] == ']'){ if (s.empty()) return false; switch (s.top()) { case '(': if (exp[i] == ')') {s.pop(); break;} else{ return false; } case '{': if (exp[i] == '}') {s.pop(); break;} else{ return false; } case '[': if (exp[i] == ']') {s.pop(); break;} else{ return false; } } } } return s.empty()?true:false; }
If you are waiting for the mentors to reply then they ain't goint to reply and apart from that yeah coding style looks good
Here's my code. I implemented using stack . The stack is implemented using object oriented implementation using arrays.
`// Stack - ||Object oriented implementation|| using arrays
#include
using namespace std;
#define MAX_SIZE 4
class Stack
{
private:
int A[MAX_SIZE]; // array to store the stack
int top; // variable to mark the top index of stack.
public:
// constructor
Stack()
{
top = -1; // for empty array, set top = -1
}
};
int solve(){
Stack S;
char ch=getchar();
while(ch!='\n'){
if(ch=='{' || ch=='(' || ch=='['){
S.Push(ch);
}
else if(ch=='}' || ch==')' || ch==']'){
if(S.IsEmpty() || (S.Top()!='{' && ch=='}') || S.Top()!='(' && ch==')' || S.Top()!='[' && ch==']');
else S.Pop();
}
ch=getchar();
}
S.IsEmpty()?printf("Balanced\n"):printf("Not Balanced\n");
return 0;
}
int main()
{
printf("Enter string of braces:\n");
solve();
return 0;
}`