Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,7 @@ Usage:
This program will go to the challenge you are testing and pass in the testfiles
within that challenge to your solution. Your solution must be named solution
followed by the extension of your choice (e.g. solution.py, solution.exe, etc). Your
program must expect a line(s) of standard input and the solution must be printed out and
terminate with a new line (most print functions will do this automatically)
program must expect a line(s) of standard input and the solution must be printed out.

######Sample Program

Expand Down
3 changes: 1 addition & 2 deletions _bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ if [[ "$#" == "0" ]]; then
This program will go to the challenge you are testing and pass in the testfiles
within that challenge to your solution. Your solution must be named solution
followed by the extension of your choice (e.g. solution.py, solution.exe, etc). Your
program must expect a line of standard input and the solution must be printed out and
terminate with a new line.
program must expect a line of standard input and the solution must be printed out.

EOS
exit 0
Expand Down
10 changes: 10 additions & 0 deletions challenge_10/C/karanchawla/README.md
Original file line number Diff line number Diff line change
@@ -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.

171 changes: 171 additions & 0 deletions challenge_10/C/karanchawla/challenge10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
karanchawla
Challenge #10
Check for balanced paranthesis
Using stack to check if the matching pair exists or not
*/

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#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;
}