diff --git a/day0/Kalpesh/1/ppm.c b/day0/Kalpesh/1/ppm.c new file mode 100644 index 0000000..c6050f8 --- /dev/null +++ b/day0/Kalpesh/1/ppm.c @@ -0,0 +1,130 @@ +#include +#include + +#define P6 + +typedef struct{ + unsigned char r,g,b; +}Color; + +typedef struct{ + int x,y; + int r; +}Circle; + +typedef struct{ + int x,y; + int w,h; +}Rect; + +int readIntFromBuffer(char * buffer, int * pos){ + int a = 0; + int index = *pos; + while (buffer[index] == '\n' || buffer[index] == ' ' || buffer[index] == '\r'){ + (index)++; + } + while (buffer[index] != '\n' && buffer[index] != ' ' && buffer[index] != 0 && buffer[index] != '\r'){ + if (buffer[index] >= '0' && buffer[index] <= '9'){ + a = 10 * a + (buffer[index] - '0'); + } + + (index)++; + } + *pos = index; + return(a); +} + +char * loadFileToBuffer(const char * filePath, FILE * f){ + fopen_s(&f, filePath, "rb"); + if (!f){ + printf("No file"); + return(0); + } + fseek(f, 0, SEEK_END); + long long int size = ftell(f); + fseek(f, 0, SEEK_SET); + char * buffer = (char *)malloc((size + 1) * sizeof(char)); + + fread(buffer, 1, size, f); + buffer[size] = 0; + + fclose(f); + return(buffer); +} + + +void main(){ + FILE* f; + + char * buffer = loadFileToBuffer("render.txt", f); + + int i = 0; + int columns = readIntFromBuffer(buffer,&i); + int rows = readIntFromBuffer(buffer,&i); + + + Color * pixelData = (Color *) malloc(rows * columns * sizeof(Color)); + Color current; + while (*(buffer+i) != 0){ + if (*(buffer+i) == 'r'){ + Rect r; + i++; + r.x = readIntFromBuffer(buffer,&i); + r.y = readIntFromBuffer(buffer,&i); + r.w = readIntFromBuffer(buffer,&i); + r.h = readIntFromBuffer(buffer,&i); + current.r = readIntFromBuffer(buffer,&i); + current.g = readIntFromBuffer(buffer,&i); + current.b = readIntFromBuffer(buffer,&i); + for (int i=r.x - 0.5 * r.w; i<= r.x + 0.5 * r.w; i++){ + for (int j = r.y - 0.5 * r.h; j<= r.y + 0.5 * r.h; j++){ + if (i>=0 && j>=0 && i=0 && j>=0 && i +#include + +typedef struct{ + double * array; + int size; + int top; +}Stack; + +void createStack(Stack * stack, int size){ + stack->size = size; + stack->array = (double *)malloc(size*sizeof(double)); + stack->top = -1; +} + + +void StackDump(Stack * stack){ + for (int i=0; i<=stack->top; i++){ + printf("%.2f\t", stack->array[i]); + } + printf("\n"); +} + +void push(Stack * stack, double data){ + stack->top++; + if (stack->top == stack->size){ + + int newSize = stack->size * 1.7 + 1; + // printf("[Realloc increase] Old size: %d New size: %d\n", stack->size, newSize); + double *newArray = (double *)malloc(newSize * sizeof(double)); + for (int i=0; i< stack->top; i++){ + newArray[i] = stack->array[i]; + } + free(stack->array); + stack->size = newSize; + stack->array = newArray; + } + stack->array[stack->top] = data; +} + +double pop(Stack * stack){ + if (stack->top == -1){ + return(0.0f); + } + double top = stack->array[stack->top]; + stack->top--; + if ((stack->top) < (stack->size)*0.6){ + int newSize = stack->size * 0.8 + 1; + // printf("[Realloc decrease] Old size: %d New size: %d\n", stack->size, newSize); + double *newArray = (double *)malloc(newSize * sizeof(double)); + for (int i=0; i<= stack->top; i++){ + newArray[i] = stack->array[i]; + } + free(stack->array); + stack->size = newSize; + stack->array = newArray; + } + return(top); + +} + +char * loadFileToBuffer(const char * filePath, FILE * f){ + fopen_s(&f, filePath, "rb"); + if (!f){ + printf("No file"); + return(0); + } + fseek(f, 0, SEEK_END); + long long int size = ftell(f); + fseek(f, 0, SEEK_SET); + char * buffer = (char *)malloc((size + 1) * sizeof(char)); + + fread(buffer, 1, size, f); + buffer[size] = 0; + + fclose(f); + return(buffer); +} + +int isNumber(char c){ + return((c >= '0')&& (c<= '9')); +} + +int isOperator(char c){ + return((c == '+')||(c == '-')||(c == '*')||(c == '/')); +} + +int readIntFromBuffer(char * buffer, int * pos){ + int a = 0; + int index = *pos; + while (buffer[index] == '\n' || buffer[index] == ' ' || buffer[index] == '\r'){ + (index)++; + } + while (buffer[index] != '\n' && buffer[index] != ' ' && buffer[index] != 0 && buffer[index] != '\r' && !isOperator(buffer[index])){ + if (buffer[index] >= '0' && buffer[index] <= '9'){ + a = 10 * a + (buffer[index] - '0'); + } + + (index)++; + } + *pos = index; + return(a); +} + + +int main (){ + Stack stack; + createStack(&stack, 8); + + FILE * f = NULL; + char * buffer = loadFileToBuffer("expressions.txt", f); + + int i = 0; + fopen_s(&f, "results.txt", "w"); + while (buffer[i]){ + if (isNumber(buffer[i])){ + push(&stack, (double)readIntFromBuffer(buffer, &i)); + } + if (isOperator(buffer[i])){ + if (stack.top < 1){ + printf("Invalid expression\n"); + return(-1); + } + double a = pop(&stack); + double b = pop(&stack); + // printf("[EVALUATE]: "); + + switch (buffer[i]){ + case '+': + push(&stack, (double)b+a); + break; + case '-': + push(&stack, (double)b-a); + break; + case '*': + push(&stack, (double)b*a); + break; + case '/': + push(&stack, (double)b/a); + break; + default: + break; + } + i++; + } + if (buffer[i] == '\n' || buffer[i] == 0){ + double result = pop(&stack); + if (stack.top == -1){ + fprintf(f, "%0.6f\n", result); + } + } + i++; + } + fclose(f); + free(buffer); + + return(0); +} \ No newline at end of file diff --git a/day1/Kalpesh/airports.c b/day1/Kalpesh/airports.c new file mode 100644 index 0000000..79c07be --- /dev/null +++ b/day1/Kalpesh/airports.c @@ -0,0 +1,216 @@ +#include +#include +#include + +#define AIRPORTS 15 +#define slotsNo 15 + + +typedef struct ValueNode{ + const char * value; + struct ValueNode * next; + uint32_t keyHash; +}ValueNode; + +typedef struct{ + ValueNode * slots[slotsNo]; +}HashTable; + + +uint32_t adler32(const char * key, int len){ + uint32_t a = 1, b = 0; + for (int i = 0; i= 'a' && c<= 'z')|| (c >= 'A' && c<= 'Z')); +} + +const char * readStringFromBuffer(char * buffer, int * pos){ + while (!isAlphabet(buffer[*pos])){ + (*pos)++; + } + const char * a = buffer + *pos; + while (!isDelimiter(buffer[*pos])){ + (*pos)++; + } + // changing delimiter to 0 + buffer[(*pos)++] = 0; + + return(a); +} + +// sets all slots to point to NULL +void initHashTable(HashTable * hashTable){ + for (int i=0; islots[i] = NULL; + } +} + +void addToHashTable(HashTable *hashtable ,const char * key, const char * value){ + uint32_t keyHash = adler32(key, len(key)); + int index = keyHash % slotsNo; + + ValueNode * val = (ValueNode*)malloc(sizeof(ValueNode)); + val->value = value; + val->keyHash = keyHash; + val->next = NULL; + + if (!hashtable->slots[index]){ + hashtable->slots[index] = val; + } + else{ + printf("COLLISION: index:%d\n", index); + ValueNode * a = hashtable->slots[index]; + while (a->next){ + a = a->next; + } + a->next = val; + } + +} + +void emptyHashTable(HashTable * hashtable){ + for (int i=0; i< slotsNo; i++){ + if (hashtable->slots[i]){ + ValueNode *a, * b = hashtable->slots[i]; + while (b){ + a = b->next; + free(b); + b = a; + } + hashtable->slots[i] = NULL; + } + } +} + + +void hashTableDump(HashTable * hashtable){ + for (int i = 0; islots[i]; + while (a){ + printf("%s\t", a->value); + a = a->next; + } + printf("\n"); + } +} + + +ValueNode * getNodeFromHashtable(HashTable * hashtable, const char * key){ + uint32_t keyhash = adler32(key, len(key)); + int index = keyhash%slotsNo; + + ValueNode * val = hashtable->slots[index]; + while (val){ + if (val->keyHash == keyhash){ + return(val); + } + val = val->next; + } + return(NULL); +} + + + +char * loadFileToBuffer(const char * filePath, FILE * f){ + fopen_s(&f, filePath, "rb"); + if (!f){ + printf("No file"); + return(0); + } + fseek(f, 0, SEEK_END); + int size = ftell(f); + fseek(f, 0, SEEK_SET); + char * buffer = (char *)malloc((size + 1) * sizeof(char)); + + fread(buffer, 1, size, f); + buffer[size] = 0; + + fclose(f); + return(buffer); +} + +void maxFlights(const char ** airports, HashTable * h){ + int max = 0; + const char * winningAirport = ""; + for (int i=0; i< AIRPORTS;i++){ + int flights = 1; + ValueNode * current = getNodeFromHashtable(h, airports[i]); + if (current) { + uint32_t hash = current->keyHash; + while (current->next && (current->next->keyHash == hash)){ + flights++; + current = current->next; + } + if (max <= flights){ + max = flights; + winningAirport = airports[i]; + } + } + } + + FILE * f; + fopen_s(&f, "airport_output.csv", "w"); + fprintf(f, "%d",max); + ValueNode *winning = getNodeFromHashtable(h, winningAirport); + for(int i=0; i< max; i++){ + fprintf(f,",\"%s\"",winning->value); + winning = winning->next; + } + fclose(f); +} + + + +void main(){ + HashTable h; + initHashTable(&h); + FILE * f = NULL; + char *bufferA = loadFileToBuffer("airport_a.txt", f); + const char * airports[AIRPORTS]; + int index =0; + for (int i=0; i<15; i++){ + airports[i] = readStringFromBuffer(bufferA, &index); + } + + + index = 0; + char * bufferB = loadFileToBuffer("airport_b.csv",f); + const char * to; + const char * from; + while (bufferB[index]){ + from = readStringFromBuffer(bufferB, &index); + to = readStringFromBuffer(bufferB, &index); + addToHashTable(&h, to, from); + } + // hashTableDump(&h); + + maxFlights(airports, &h); + + emptyHashTable(&h); + + if (bufferA) + free(bufferA); + if (bufferB) + free(bufferB); +} \ No newline at end of file diff --git a/day1/Kalpesh/derivative.c b/day1/Kalpesh/derivative.c new file mode 100644 index 0000000..7e769c3 --- /dev/null +++ b/day1/Kalpesh/derivative.c @@ -0,0 +1,222 @@ +#include +#include + +typedef struct Term{ + int coeff; + int pow; +}Term; + +typedef struct Node{ + Term term; + struct Node *next; +}Node; + + +typedef struct LinkedList{ + Node * head; +}LinkedList; + +void initList(LinkedList *l){ + l->head = NULL; +} + +void applyPowerRule(Term *t){ + t->coeff *= t->pow; + t->pow--; +} + +Node * newNode(Term t){ + Node *a = (Node *)malloc(sizeof(Node)); + a->term = t; + a->next = NULL; + return(a); +} + +void deleteNode(Node *node){ + free(node); +} + +Node * getNodeFromList(LinkedList *l, int pos){ + Node * a = l->head; + for(int i=0; inext; + } + return(a); +} + +void insertToList(LinkedList *list, Node* node){ + if (!list->head){ + list->head = node; + } + else{ + Node * a = list->head; + Node * prev = NULL; + while (a){ + if (node->term.pow > a->term.pow){ + node->next = a; + if (a == list->head) { + list->head = node; + } + else { + prev->next = node; + } + return; + } + else if (node->term.pow == a->term.pow){ + a->term.coeff += node->term.coeff; + deleteNode(node); + return; + } + else{ + prev = a; + a = a->next; + } + } + prev->next = node; + } +} + +void emptyList(LinkedList * l){ + Node * a = l->head; + Node * b = NULL; + while (a){ + b = a->next; + free(a); + a = b; + } + l->head = NULL; +} + +int isOperator(char c){ + return((c == '+' || c=='-'|| c=='*'||c=='/')); +} + +int isNumber(char c){ + return((c >= '0')&&(c<='9')); +} + +int isDelimiter(char c) { + return((c == 0) || (c == '\n')); +} + +int getIntFromBuffer(char * buffer, int *index, char delimit){ + int multiplier = 1; + int a = 0; + while (!isNumber(buffer[*index])){ + if (buffer[*index] == '-') + multiplier = -1; + if (buffer[*index] == delimit){ + a = 1; + break; + } + (*index)++; + } + + while (isNumber(buffer[*index])){ + a = a*10 + (buffer[*index]-'0'); + (*index)++; + } + return(multiplier * a); +} + +char * loadFileToBuffer(const char * filePath){ + FILE * f = NULL; + fopen_s(&f, filePath, "rb"); + if (!f){ + printf("No file"); + return(0); + } + fseek(f, 0, SEEK_END); + int size = ftell(f); + fseek(f, 0, SEEK_SET); + char * buffer = (char *)malloc((size + 1) * sizeof(char)); + + fread(buffer, 1, size, f); + buffer[size] = 0; + + fclose(f); + return(buffer); +} + + +Term parseTerm(char * buffer, int * index){ + Term t; + + t.coeff = getIntFromBuffer(buffer, index, 'x'); + + while (buffer[*index] != 'x'){ + (*index)++; + if (isOperator (buffer[*index]) || isDelimiter(buffer[*index])){ + t.pow = 0; + return(t); + } + } + while (!isNumber(buffer[*index])){ + (*index)++; + if (isOperator (buffer[*index]) || isDelimiter(buffer[*index])){ + t.pow = 1; + return(t); + } + } + + t.pow = getIntFromBuffer(buffer, index, ' '); + if (buffer[*index] == '\r') + (*index)++; + return(t); +} + + +void computeDerivative(LinkedList *l){ + Node *term = l->head; + while (term){ + applyPowerRule(&term->term); + term = term->next; + } +} + +void writeToFile(Term term, FILE *f, int sign){ + if (sign) + fprintf(f, (term.coeff <0)?" ":" + "); + if (term.coeff == 0){ + fprintf(f, "0"); + } + else if (term.pow == 0){ + fprintf(f,"%d", term.coeff); + } + else if (term.pow == 1){ + fprintf(f,"%dx", term.coeff); + } + else{ + fprintf(f, "%dx^%d", term.coeff, term.pow); + } +} + +void main(){ + LinkedList l; + l.head = NULL; + char * buffer = loadFileToBuffer("polynomial_derivative.csv"); + int index = 0; + FILE * f = NULL; + fopen_s(&f, "polynomial_derivative_out.txt", "w"); + while (buffer[index]){ + Node * term = newNode(parseTerm(buffer, &index)); + insertToList(&l, term); + if (buffer[index] == '\n' || buffer[index] == 0){ + computeDerivative(&l); + writeToFile(l.head->term, f, 0); + Node* a = l.head->next; + while (a) { + writeToFile(a->term, f, 1); + a = a->next; + } + fprintf(f, "\n"); + emptyList(&l); + if (buffer[index] == 0) + break; + index++; + } + } + fclose(f); + if (buffer) + free(buffer); +} \ No newline at end of file diff --git a/day1/Kalpesh/infix_calc.c b/day1/Kalpesh/infix_calc.c new file mode 100644 index 0000000..59f248c --- /dev/null +++ b/day1/Kalpesh/infix_calc.c @@ -0,0 +1,233 @@ +#include +#include + +typedef struct Node{ + union{ + float data; + int operator; + }; + int NodeType; + struct Node * left, *right; +}Node; + +typedef struct Tree{ + Node * root; + int height; +}Tree; + +int isOperator(char c){ + return((c == '+' || c=='-'|| c=='*'||c=='/')); +} + +int isNumber(char c){ + return((c >= '0')&&(c<='9')); +} + +int getIntFromBuffer(char * buffer, int *index){ + while (!isNumber(buffer[*index]) && buffer[*index]==' '){ + (*index)++; + } + int a = 0; + while (isNumber(buffer[*index])){ + a = a*10 + (buffer[*index]-'0'); + (*index)++; + } + return(a); +} + +// lmao +int precedence(char operator){ + if (operator == '+') + return(0); + if (operator == '-') + return(1); + if (operator == '*') + return(2); + if (operator == '/') + return(3); + return(-1); +} + + + +void TreeDump(Node * start){ + if (start){ + if (start->NodeType == 1) + printf("%c\t", start->operator); + else { + printf("%f\t", start->data); + } + TreeDump(start->left); + TreeDump(start->right); + } +} + +Node * searchForParent(Node *root, Node *child){ + if (root){ + if(root->left && root->left == child){ + return(root); + } + else if (root->right && root->right == child){ + return(root); + } + else{ + Node * a = searchForParent(root->left, child); + if(a){ + return(a); + } + a = searchForParent(root->right, child); + if(a){ + return(a); + } + } + } + return(NULL); +} + +void deleteNode(Node * node){ + free(node); +} + +void deleteTree(Node * root){ + if (root->left){ + deleteTree(root->left); + } + if (root->right){ + deleteTree(root->right); + } + deleteNode(root); +} + + +void evaluate(Node * root){ + if (root && root->NodeType == 1){ + if (root->left->NodeType == 1){ + evaluate(root->left); + } + if (root->right->NodeType == 1){ + evaluate(root->right); + } + if (root->left->NodeType == 0 && root->right->NodeType == 0){ + switch (root->operator){ + case '+': + root->data = root->left->data + root->right->data; + break; + case '-': + root->data = root->left->data - root->right->data; + break; + case '*': + root->data = root->left->data * root->right->data; + break; + case '/': + root->data = root->left->data / root->right->data; + break; + default: + break; + } + root->NodeType = 0; + } + + } +} + + + + +void main(){ + Tree t; + t.root = NULL; + t.height = 0; + // no input file :stab: + char * buffer = "1-6*9+5/9-1+2\n12/2+6*4-2"; + int index = 0; + Node * prev = NULL; + Node * a = NULL; + FILE * f = NULL; + fopen_s(&f, "infix_output.txt", "w"); + while (buffer[index] != 0){ + if (!t.root){ + a = (Node *)malloc(sizeof(Node)); + a->data = getIntFromBuffer(buffer, &index); + a->left = NULL; + a->right = NULL; + a->NodeType = 0; + prev = NULL; + } + + Node * current = NULL; + + // read operator + current = (Node *)malloc(sizeof(Node)); + current->operator = buffer[index]; + current->NodeType = 1; + current->left = NULL; + index++; + + // read second operand + Node * b = (Node *)malloc(sizeof(Node)); + b->data = getIntFromBuffer(buffer, &index); + b->left = NULL; + b->right = NULL; + b->NodeType = 0; + current->right = b; + + // first tree edge + if (!t.root){ + current->left = a; + t.root = current; + } + // check precedence with previous operators + if (prev){ + // if current has more precedence, add as a child to prev + if (precedence(current->operator) > precedence(prev->operator)){ + current->left = prev->right; + prev->right = current; + } + else{ + Node *parent = prev; + // if current has less precedence, add as parent to parent + while (parent && (precedence(current->operator) <= precedence(parent->operator))){ + if (current->left) + parent->right = current->left; + current->left = parent; + if (t.root == parent){ + t.root = current; + break; + } + else{ + Node * gp = searchForParent(t.root, parent); + // if grandparent exists, replace pointer to parent with current + if (gp){ + if(gp->left == parent){ + gp->left = current; + } + else{ + gp->right = current; + } + } + parent = gp; + } + } + } + } + prev = current; + while (!isOperator(buffer[index])){ + if (buffer[index] == '\n' || buffer[index] == 0){ + if (t.root){ + evaluate(t.root); + printf("%f\n", t.root->data); + fprintf(f,"%f\n", t.root->data); + deleteTree(t.root); + t.root = NULL; + } + if (buffer[index]) + index++; + break; + } + index++; + } + } + + fclose(f); + // free(buffer); +} diff --git a/day1/Kalpesh/mergesort.c b/day1/Kalpesh/mergesort.c new file mode 100644 index 0000000..3587cb8 --- /dev/null +++ b/day1/Kalpesh/mergesort.c @@ -0,0 +1,161 @@ +#include +#include + +#define MIN(a,b) ((aarr = (int *) malloc(capacity * sizeof(int)); + a->capacity = capacity; + a->size = 0; +} + +void deleteArray(Array *a){ + free(a->arr); + a->capacity = 0; + a->size = 0; +} + +void resizeArray(Array *a, int newSize){ + int *newArray = (int *)malloc(newSize * sizeof(int)); + for (int i=0; i< MIN(newSize, a->size); i++){ + newArray[i] = a->arr[i]; + } + free(a->arr); + a->arr = newArray; + a->capacity = newSize; + if (a->size > a->capacity) + a->size = a->capacity; +} + +void addtoArray(Array* a, int data) { + // resize if size is small + if (a->size < 0.3 * a->capacity){ + int newSize = a->capacity * 0.3; + resizeArray(a, newSize); + } + + // full + if (a->size == a->capacity) { + int newSize = a->capacity * 1.5 + 1; + resizeArray(a, newSize); + } + + a->arr[a->size++] = data; +} + +void mergeArray(int * arr, int start, int mid, int end){ + int n1 = mid - start +1; + int n2 = end - mid; + int *a = (int *)malloc(n1 * sizeof(int)); + int *b = (int *)malloc(n2 * sizeof(int)); + + for (int i=0; i< n1; i++){ + a[i] = arr[start + i]; + } + for (int i=0; i< n2; i++){ + b[i] = arr[mid + 1 + i]; + } + int i=0,j=0,k=start; + while (i= '0')&& (c<= '9')); +} + +int readIntFromBuffer(char * buffer, int * pos){ + int a = 0; + int index = *pos; + while (!isNumber(buffer[index])){ + (index)++; + } + while (isNumber(buffer[index])){ + a = 10 * a + (buffer[index] - '0'); + (index)++; + } + *pos = index; + return(a); +} + +char * loadFileToBuffer(const char * filePath, FILE * f){ + fopen_s(&f, filePath, "rb"); + if (!f){ + printf("No file"); + return(0); + } + fseek(f, 0, SEEK_END); + long long int size = ftell(f); + fseek(f, 0, SEEK_SET); + char * buffer = (char *)malloc((size + 1) * sizeof(char)); + + fread(buffer, 1, size, f); + buffer[size] = 0; + + fclose(f); + return(buffer); +} + + +void main(){ + FILE * f = NULL; + char * buffer = loadFileToBuffer("mergesort_input.csv", f); + Array a; + createArray(&a, 30); + int i = 0; + fopen_s(&f, "mergesort_output.csv", "w"); + while (buffer[i]){ + if (isNumber(buffer[i])){ + int num = readIntFromBuffer(buffer, &i); + addtoArray(&a, num); + } + if (buffer[i]=='\n' || buffer[i] == 0){ + mergeSort(a.arr, 0, a.size-1); + + for (int i=0; i