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 diff --git a/challenge_5/C/karanchawla/src/README.md b/challenge_5/C/karanchawla/src/README.md new file mode 100644 index 000000000..1fa8473ca --- /dev/null +++ b/challenge_5/C/karanchawla/src/README.md @@ -0,0 +1,13 @@ +''' +Karan Chawla +Challenge #5 +''' + +# Approach + +Use random number generator along with the size of the string to shuffle characters of the input string. +Add a character to this shuffled string. +Sort the two strings and compare to return the added character. + +To Do: +[ ] Change the random position based implementation to account for the edge case when length(s) = 0 \ No newline at end of file diff --git a/challenge_5/C/karanchawla/src/challenge5.c b/challenge_5/C/karanchawla/src/challenge5.c new file mode 100644 index 000000000..9ab467a76 --- /dev/null +++ b/challenge_5/C/karanchawla/src/challenge5.c @@ -0,0 +1,120 @@ +/* +Author: Karan Chawla +Challenege #5 +Expected Running time O(k + n log n) +where k is the position of the char in the new sorted string +*/ + +#include +#include +#include +#include + + +//utility function used by qsort to sort the strings +int comp (const void * elem1, const void * elem2) +{ + char f = *((char*)elem1); + char s = *((char*)elem2); + if (f > s) return 1; + if (f < s) return -1; + return 0; +} + +//function to shuffle the string +void shuffle(char *array, size_t n) +{ + if (n > 1) + { + size_t i; + for (i = 0; i < n; i++) + { + //size_t j = i + rand() / (RAND_MAX / (n - i)); + size_t j = rand()%n ; + printf("%s\n", array); + char t = array[j]; + array[j] = array[i]; + array[i] = t; + } + } +} + +//utility function to print array +void printArray(char *a, int size) +{ + for(int i=0;i +#include + + +//node definition for the linked list +typedef struct node +{ + int data; + struct node* next; + struct node* random; +}Node; + +//typedef struct node* Node; + +//utility function to create a node +Node* newNode(int key) +{ + Node* temp = (struct node*) malloc(sizeof(struct node)); + + temp->data = key; + temp->next = NULL; + temp->random = NULL; + + return temp; +} + +//utility function to print the list +void printList(Node* head) +{ + while(head!=NULL) + { + if(head->random!=NULL) + { + printf("Node key: %d\t", head->data); + printf("Random Node pointer data: %d\n", head->random->data); + } + else + { + printf("Node key: %d\t", head->data); + printf("Random Node pointer data: NULL\n"); + } + head = head->next; + } + return; +} + +//First step is to insert a copy of each node +//after the first node in the original list +//Copy the random pointers +//Separate this second list +Node* deepCopy(Node* head) +{ + Node* old; + Node* temp = head; + //modify this list + while(temp) + { + + Node* temp2 = newNode(temp->data); + old = temp->next; + temp->next = temp2; + temp2->next = old; + temp = old; + } + + temp = head; + //copy arbitrary matching + while(temp && temp->next) + { + if(temp->random==NULL) + { + temp->next->random = NULL; + } + else + temp->next->random = temp->random->next; + + temp = temp->next->next; + } + + //Separating the two lists + Node* newNode = head->next; + temp = head; + + while(temp) + { + old = temp->next; + temp->next = old->next; + temp = old->next; + if(old->next) old->next->next; + } + + return newNode; +} + +//Driver program; +int main(void) +{ + Node* head = newNode(1); + head->next = newNode(2); + head->next->next = newNode(3); + head->next->next->next = newNode(4); + head->next->next->next->next = newNode(5); + head->next->next->next->next->next = NULL; + + // Assign random Pointers + head->random = head->next->next; + head->next->random = head->next->next->next; + head->next->next->random = NULL; + head->next->next->next->random = head->next; + head->next->next->next->next->random = head->next->next->next->next; + + printList(head); + Node* deepCopyList = deepCopy(head); + + printList(deepCopyList); + + return 0; +} \ No newline at end of file