From b4e0e783bc1c450b416491ef1e2295c11991dd7d Mon Sep 17 00:00:00 2001 From: Karan Chawla Date: Tue, 10 Jan 2017 08:25:46 -0600 Subject: [PATCH 1/3] [C] Challenge 10(Unreviewed) --- challenge_10/C/karanchawla/challenge10.c | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge_10/C/karanchawla/challenge10.c b/challenge_10/C/karanchawla/challenge10.c index d057006e0..12aaf1cbb 100644 --- a/challenge_10/C/karanchawla/challenge10.c +++ b/challenge_10/C/karanchawla/challenge10.c @@ -22,7 +22,6 @@ Using stack to check if the matching pair exists or not #define TOO_LONG 2 #endif - //structure for linked list node typedef struct node { From 34e888bbb3787c7ddc857fecd21a9549d76462a3 Mon Sep 17 00:00:00 2001 From: Karan Chawla Date: Tue, 10 Jan 2017 09:13:59 -0600 Subject: [PATCH 2/3] Deleted 10 --- challenge_10/C/karanchawla/README.md | 10 -- challenge_10/C/karanchawla/challenge10.c | 171 ----------------------- 2 files changed, 181 deletions(-) delete mode 100644 challenge_10/C/karanchawla/README.md delete mode 100644 challenge_10/C/karanchawla/challenge10.c diff --git a/challenge_10/C/karanchawla/README.md b/challenge_10/C/karanchawla/README.md deleted file mode 100644 index 2f3ce0769..000000000 --- a/challenge_10/C/karanchawla/README.md +++ /dev/null @@ -1,10 +0,0 @@ -''' -Karan Chawla -Challenge #10 -''' - -This was an easy one. I used a stack to push the first ocurrences of open paranthesis and when a -closing paranthesis is encountered - popped an element from the stack and compared it to the -current element. If they "match"(for eg: '(' with ')') for every occurence - the program returns -True else returns False. - diff --git a/challenge_10/C/karanchawla/challenge10.c b/challenge_10/C/karanchawla/challenge10.c deleted file mode 100644 index 12aaf1cbb..000000000 --- a/challenge_10/C/karanchawla/challenge10.c +++ /dev/null @@ -1,171 +0,0 @@ -/* -karanchawla -Challenge #10 -Check for balanced paranthesis -Using stack to check if the matching pair exists or not -*/ - -#include -#include -#include -#include - -#ifndef OK -#define OK 0 -#endif - -#ifndef NO_INPUT -#define NO_INPUT 1 -#endif - -#ifndef TOO_LONG -#define TOO_LONG 2 -#endif - -//structure for linked list node -typedef struct node -{ - int data; - struct node* next; -}Node; - -//utility function to create a new node -Node* newNode(int x) -{ - Node* stackNode = (Node*) malloc(sizeof(Node)); - stackNode->data = x; - stackNode->next = NULL; - return stackNode; -} - -//utility function to check if linked list is empty -int isEmpty(Node* head) -{ - if(head->next==NULL) - return 1; - return 0; -} - -//utility function to push a node to linked list -void push(Node** root, int x) -{ - struct node* stackNode = newNode(x); - stackNode->next = *root; - *root = stackNode; - //use for debugging - //printf("Pushed %d to the stack\n",x); -} - -//utility function pop a node from the stack -char pop(Node** root) -{ - if(isEmpty(*root)) - return INT_MIN; - - Node* temp = *root; - *root = (*root)->next; - char popped = temp->data; - - free(temp); - return popped; -} - -//bool function to check if it's a matching pair or not -int isMatchingPair(char c1, char c2) -{ - if(c1== '(' && c2 == ')') - return 1; - else if(c1 == '[' && c2 ==']') - return 1; - else if(c1 == '{' && c2 =='}') - return 1; - else if(c1 == '<' && c2 =='>') - else - return 0; -} - -//function that checks for balanced paranthesis -int checkParathesisBalanced(char *str) -{ - int size = strlen(str); - Node* stack = NULL; - int i=0; - - while (str[i]) - { - if(str[i]=='[' || str[i]=='(' || str[i]=='{' || str[i]=='<') - { - push(&stack,(int)str[i]); - } - - if(str[i]=='}' || str[i]==')' || str[i]==']' || str[i]=='>') - { - if (stack==NULL) - return 0; - - else if (!isMatchingPair(2pop(&stack),str[i])) - return 0; - } - i++; - } - - if (stack==NULL) - return 1; - else - return 0; -} - -//utility function to safely get string input from user -static int getLine (char *prmpt, char *buff, size_t sz) { - int ch, extra; - - // Get line with buffer overrun protection. - if (prmpt != NULL) { - printf ("%s", prmpt); - fflush (stdout); - } - if (fgets (buff, sz, stdin) == NULL) - return NO_INPUT; - - // If it was too long, there'll be no newline. In that case, we flush - // to end of line so that excess doesn't affect the next call. - if (buff[strlen(buff)-1] != '\n') { - extra = 0; - while (((ch = getchar()) != '\n') && (ch != EOF)) - extra = 1; - return (extra == 1) ? TOO_LONG : OK; - } - - // Otherwise remove newline and give string back to caller. - buff[strlen(buff)-1] = '\0'; - return OK; -} - -//Drive program. Takes a string 'buff' -//from the user and checks for balanced paranthesis -int main(void) -{ - int out; - char buff[10000]; - - out = getLine ("Enter string> ", buff, sizeof(buff)); - if (out == NO_INPUT) { - // Extra NL since my system doesn't output that on EOF. - printf ("\nNo input\n"); - return 1; - } - - if (out == TOO_LONG) { - printf ("Input too long [%s]\n", buff); - return 1; - } - - printf ("OK [%s]\n", buff); - - if (checkParathesisBalanced(buff)) - printf("\n True"); - else - printf("\n False "); - - return 0; -} \ No newline at end of file From 617d257ea0452bb222a47659f3bd4bdf0cc93dcb Mon Sep 17 00:00:00 2001 From: Karan Chawla Date: Tue, 10 Jan 2017 09:15:33 -0600 Subject: [PATCH 3/3] [C] Challenge 10(Unreviewed) --- challenge_10/C/karanchawla/README.md | 10 ++ challenge_10/C/karanchawla/challenge10.c | 171 +++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 challenge_10/C/karanchawla/README.md create mode 100644 challenge_10/C/karanchawla/challenge10.c diff --git a/challenge_10/C/karanchawla/README.md b/challenge_10/C/karanchawla/README.md new file mode 100644 index 000000000..2f3ce0769 --- /dev/null +++ b/challenge_10/C/karanchawla/README.md @@ -0,0 +1,10 @@ +''' +Karan Chawla +Challenge #10 +''' + +This was an easy one. I used a stack to push the first ocurrences of open paranthesis and when a +closing paranthesis is encountered - popped an element from the stack and compared it to the +current element. If they "match"(for eg: '(' with ')') for every occurence - the program returns +True else returns False. + diff --git a/challenge_10/C/karanchawla/challenge10.c b/challenge_10/C/karanchawla/challenge10.c new file mode 100644 index 000000000..12aaf1cbb --- /dev/null +++ b/challenge_10/C/karanchawla/challenge10.c @@ -0,0 +1,171 @@ +/* +karanchawla +Challenge #10 +Check for balanced paranthesis +Using stack to check if the matching pair exists or not +*/ + +#include +#include +#include +#include + +#ifndef OK +#define OK 0 +#endif + +#ifndef NO_INPUT +#define NO_INPUT 1 +#endif + +#ifndef TOO_LONG +#define TOO_LONG 2 +#endif + +//structure for linked list node +typedef struct node +{ + int data; + struct node* next; +}Node; + +//utility function to create a new node +Node* newNode(int x) +{ + Node* stackNode = (Node*) malloc(sizeof(Node)); + stackNode->data = x; + stackNode->next = NULL; + return stackNode; +} + +//utility function to check if linked list is empty +int isEmpty(Node* head) +{ + if(head->next==NULL) + return 1; + return 0; +} + +//utility function to push a node to linked list +void push(Node** root, int x) +{ + struct node* stackNode = newNode(x); + stackNode->next = *root; + *root = stackNode; + //use for debugging + //printf("Pushed %d to the stack\n",x); +} + +//utility function pop a node from the stack +char pop(Node** root) +{ + if(isEmpty(*root)) + return INT_MIN; + + Node* temp = *root; + *root = (*root)->next; + char popped = temp->data; + + free(temp); + return popped; +} + +//bool function to check if it's a matching pair or not +int isMatchingPair(char c1, char c2) +{ + if(c1== '(' && c2 == ')') + return 1; + else if(c1 == '[' && c2 ==']') + return 1; + else if(c1 == '{' && c2 =='}') + return 1; + else if(c1 == '<' && c2 =='>') + else + return 0; +} + +//function that checks for balanced paranthesis +int checkParathesisBalanced(char *str) +{ + int size = strlen(str); + Node* stack = NULL; + int i=0; + + while (str[i]) + { + if(str[i]=='[' || str[i]=='(' || str[i]=='{' || str[i]=='<') + { + push(&stack,(int)str[i]); + } + + if(str[i]=='}' || str[i]==')' || str[i]==']' || str[i]=='>') + { + if (stack==NULL) + return 0; + + else if (!isMatchingPair(2pop(&stack),str[i])) + return 0; + } + i++; + } + + if (stack==NULL) + return 1; + else + return 0; +} + +//utility function to safely get string input from user +static int getLine (char *prmpt, char *buff, size_t sz) { + int ch, extra; + + // Get line with buffer overrun protection. + if (prmpt != NULL) { + printf ("%s", prmpt); + fflush (stdout); + } + if (fgets (buff, sz, stdin) == NULL) + return NO_INPUT; + + // If it was too long, there'll be no newline. In that case, we flush + // to end of line so that excess doesn't affect the next call. + if (buff[strlen(buff)-1] != '\n') { + extra = 0; + while (((ch = getchar()) != '\n') && (ch != EOF)) + extra = 1; + return (extra == 1) ? TOO_LONG : OK; + } + + // Otherwise remove newline and give string back to caller. + buff[strlen(buff)-1] = '\0'; + return OK; +} + +//Drive program. Takes a string 'buff' +//from the user and checks for balanced paranthesis +int main(void) +{ + int out; + char buff[10000]; + + out = getLine ("Enter string> ", buff, sizeof(buff)); + if (out == NO_INPUT) { + // Extra NL since my system doesn't output that on EOF. + printf ("\nNo input\n"); + return 1; + } + + if (out == TOO_LONG) { + printf ("Input too long [%s]\n", buff); + return 1; + } + + printf ("OK [%s]\n", buff); + + if (checkParathesisBalanced(buff)) + printf("\n True"); + else + printf("\n False "); + + return 0; +} \ No newline at end of file