Last active
September 5, 2020 09:18
-
-
Save magiskboy/48e595be0d3a81fb8e80eb9b5b2a6170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <string.h> | |
char* TERMS[] = {"()", "[]", "{}"}; | |
struct node_t { | |
char data; | |
struct node_t* next; | |
}; | |
struct node_t* push_stack(char c, struct node_t* stack) { | |
struct node_t* new_node = (struct node_t*)malloc(sizeof(struct node_t)); | |
new_node->data = c; | |
new_node->next = stack; | |
return new_node; | |
} | |
struct node_t* pop_stack(struct node_t* stack) { | |
struct node_t* del_node = stack; | |
stack = del_node->next; | |
free(del_node); | |
return stack; | |
} | |
bool empty_stack(struct node_t* stack) { | |
if (stack == NULL) return true; | |
return false; | |
} | |
bool is_open_term(char open_term) { | |
for (int i = 0; i < 3; ++i) { | |
if (TERMS[i][0] == open_term) { | |
return true; | |
} | |
} | |
return false; | |
} | |
bool match(char open_term, char close_term) { | |
for (int i = 0; i < 3; ++i) { | |
if (TERMS[i][0] == open_term) { | |
if (TERMS[i][1] == close_term) return true; | |
return false; | |
} | |
} | |
return false; | |
} | |
bool is_balanced(char* expression) { | |
struct node_t* st = NULL; | |
int n = strlen(expression); | |
char c; | |
for (int i = 0; i < n; ++i) { | |
c = expression[i]; | |
if (is_open_term(c)) { | |
st = push_stack(c, st); | |
} | |
else { | |
if (empty_stack(st)) { | |
return false; | |
} | |
char t = st->data; | |
if (!match(t, c)) { | |
return false; | |
} | |
st = pop_stack(st); | |
} | |
} | |
return empty_stack(st); | |
} | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
perror("Please pass string to program\n"); | |
exit(-1); | |
} | |
if (is_balanced(argv[1])) { | |
printf("Balanced\n"); | |
} | |
else { | |
printf("Not balanced\n"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stack> | |
const char* TERMS[] = {"()", "{}", "[]"}; | |
bool match(char open_term, char close_term) { | |
for (int i = 0; i < 3; ++i) { | |
if (TERMS[i][0] == open_term) { | |
return TERMS[i][1] == close_term; | |
} | |
} | |
return false; | |
} | |
bool is_open_term(char open_term) { | |
for (int i = 0; i < 3; ++i) { | |
if (TERMS[i][0] == open_term) { | |
return true; | |
} | |
} | |
return false; | |
} | |
bool is_balanced(char* expression) { | |
std::stack<char> st; | |
for (int i = 0; i < strlen(expression); ++i) { | |
char c = expression[i]; | |
if (is_open_term(c)) { | |
st.push(c); | |
} | |
else { | |
if (st.empty()) return false; | |
char t = st.top(); st.pop(); | |
if (!match(t, c)) { | |
return false; | |
} | |
} | |
} | |
return st.empty(); | |
} | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
std::cerr << "Please pass string to program" << std::endl; | |
exit(-1); | |
} | |
if (is_balanced(argv[1])) { | |
std::cout << "Balanced" << std::endl; | |
} | |
else { | |
std::cout << "Not balanced" << std::endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Balanced { | |
static char[][] TERMS = {{'(', ')'}, {'[', ']'}, {'{', '}'}}; | |
static boolean match(char openTerm, char closeTerm) { | |
for (char[] c : TERMS) { | |
if (c[0] == openTerm) { | |
return c[1] == closeTerm; | |
} | |
} | |
return false; | |
} | |
static boolean isOpenTerm(char openTerm) { | |
for (char[] c : TERMS) { | |
if (c[0] == openTerm) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isBalanced(String expression) { | |
Stack<Character> stack = new Stack<Character>(); | |
for (char c : expression.toCharArray()) { | |
if (isOpenTerm(c)) { | |
stack.push(c); | |
} | |
else { | |
if (stack.isEmpty() || !match(stack.pop(), c)) { | |
return false; | |
} | |
} | |
} | |
return stack.isEmpty(); | |
} | |
static public void main(String[] args) { | |
if (args.length < 1) { | |
System.err.println("Please pass string to application"); | |
return; | |
} | |
if (isBalanced(args[0])) { | |
System.out.println("Balanced"); | |
} | |
else { | |
System.out.println("Not balanced"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TERMS = ["()", "[]", "{}"]; | |
function Stack() { | |
this.data = new Array(); | |
} | |
Stack.prototype.push = function (item) { | |
this.data.unshift(item); | |
} | |
Stack.prototype.pop = function() { | |
return this.data.shift(); | |
} | |
Stack.prototype.isEmpty = function() { | |
return this.data.length === 0; | |
} | |
function Balanced() { | |
} | |
Balanced.prototype.match = function(openTerm, closeTerm) { | |
for (i = 0; i < 3; ++i) { | |
if (TERMS[i][0] === openTerm) { | |
return TERMS[i][1] === closeTerm; | |
} | |
} | |
return false; | |
} | |
Balanced.prototype.isOpenTerm = function(openTerm) { | |
for (i = 0; i < 3; ++i) { | |
if (TERMS[i][0] === openTerm) { | |
return true; | |
} | |
} | |
return false; | |
} | |
Balanced.prototype.isBalanced = function(expression) { | |
let stack = new Stack(); | |
for (i = 0; i < expression.length; ++i) { | |
let c = expression[i]; | |
if (this.isOpenTerm(c)) { | |
stack.push(c); | |
} | |
else { | |
if (stack.isEmpty() || !this.match(stack.pop(), c)) { | |
return false; | |
} | |
} | |
} | |
return stack.isEmpty(); | |
} | |
test = new Balanced(); | |
console.log(test.isBalanced('[]')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from queue import LifoQueue | |
terms = [('(', ')'), ('[', ']'), ('{', '}')] | |
def match(open_term, close_term): | |
for term in terms: | |
if term[0] == open_term: | |
return term[1] == close_term | |
return False | |
def is_open_term(c): | |
for open_term, _ in terms: | |
if open_term == c: | |
return True | |
return False | |
def is_balanced(expression): | |
stack = LifoQueue() | |
for c in expression: | |
if is_open_term(c): | |
stack.put(c) | |
else: | |
if stack.empty() or not match(stack.get(), c): | |
return False | |
return stack.empty() | |
if __name__ == '__main__': | |
expression = '(()[){([{()}])})' | |
print(is_balanced(expression)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment