diff --git a/Arrays/1D Array/solution.c b/Arrays/1D Array/solution.c new file mode 100644 index 0000000..7e11772 --- /dev/null +++ b/Arrays/1D Array/solution.c @@ -0,0 +1,11 @@ +#include + +int main(){ + int num,arr[100],i; + scanf("%d",&num); + for(i=0;i=0;i--) + printf("%d\n",arr[i]);// Writing output to STDOUT +} + diff --git a/Arrays/Multi-D Array/solution.c b/Arrays/Multi-D Array/solution.c new file mode 100644 index 0000000..d19c824 --- /dev/null +++ b/Arrays/Multi-D Array/solution.c @@ -0,0 +1,34 @@ +/*#include +void main() +{ +int i,r,c,j,arr[10][10]; +scanf("%d %d ",&r,&c); +for(i=0;i +int main() +{ + int arr[100][100],m,n,i,j; + scanf("%d %d",&m,&n); + for(i=0;i + main() +{ + int roll,n,i=0,ans[100],j; + char name[100],hash[100][100]; + scanf("%d",&n); + while(n>i) + { + scanf("%d %s%*c",&roll,name); + for(j=0;name[j]!='\0';j++) + hash[roll][j]=name[j]; + i++; + } + scanf("%d",&n); + i=0; + while(n>i) + { + scanf("%d",&ans[i]); + i++; + } + for(i=0;i +int main() +{ +int Q[100],front,rear,x,size; +int N; +char T; +front=rear=0; +scanf("%d",&N); +while(N--) +{ +scanf(" %c",&T); +if(T=='E') +{ +scanf("%d",&x); +Q[rear]=x; +rear++; +size=rear-front; +printf("%d\n",size); +} +else if(T=='D') +{ +if(front==rear) +printf("1 "); +else +{ +printf("%d ",Q[front]); +Q[front]=0; +front++;} +size=rear-front; +printf("%d\n",size); + +} +} +} diff --git a/Stacks/Stacks/solution.c b/Stacks/Stacks/solution.c new file mode 100644 index 0000000..ee6e426 --- /dev/null +++ b/Stacks/Stacks/solution.c @@ -0,0 +1,85 @@ +/*lude +int top=-1,val,stack[100]; +int ch,n,i; +void pop() +{ +top=top-1; +} +void push(int stack[],int val) +{ +top=top+1; +stack[top]=val; +} +void main() +{ + scanf("%d",&n); + for(i=0;i +#include +int top=-1; +void pop() +{ +top=top-1; +} +void push(int stack[],int val) +{ +top=top+1; +stack[top]=val; +} +int main() +{ +int n,a,b,val,max; +scanf("%d",&n); +max=n; +int stack[n]; +for(int i=0;i +#include +struct node +{ +int data; +struct node* lchild; +struct node* rchild; +}; +struct node* root; +struct node* insert(struct node* root,int it) +{ + if(root==NULL) + { + root=(struct node *)malloc(sizeof(struct node)); + root->lchild=NULL; + root->rchild= NULL; + root->data = it; + return root; + } + else + { + if(it< root->data ) + root->lchild=insert(root->lchild,it); + else if(it>root->data) + root->rchild=insert(root->rchild,it); + else + printf(" Duplicate Element !! Not Allowed !!!"); + return(root); + } +} +void create(int n) +{int i,it; + for(i=0;idata); + preorder(root->lchild); + preorder(root->rchild); + } +} +void find(int m,struct node *root) +{ if(root->data==m) + preorder(root); + else if(root->datarchild); + else + find(m,root->lchild); +} + + +main() +{ int m,n; + scanf("%d",&n); + create(n); + scanf("%d",&m); + find(m,root); +} + + + diff --git a/Trees/Binary Tree/solution.c b/Trees/Binary Tree/solution.c new file mode 100644 index 0000000..b9abc83 --- /dev/null +++ b/Trees/Binary Tree/solution.c @@ -0,0 +1,77 @@ +#include +#include +struct node +{ +int data; +struct node* lchild; +struct node* rchild; +}*ptr; +struct node* root; +struct node* getnode(int val){ +struct node* temp=(struct node*)malloc(sizeof(struct node)); +temp->data=val; +temp->lchild=NULL; +temp->rchild=NULL; +return temp; +} +int getMax(int a, int b){ + if(a >= b) + return a; + else + return b; +} + +int getHeight(struct node *root){ + int leftHeight, rightHeight; + if(root == NULL) + return 0; + leftHeight = getHeight(root->lchild); + rightHeight = getHeight(root->rchild); + + return getMax(leftHeight, rightHeight) + 1; +} + + +int getDiameter(struct node *nodePtr) { + /* Empty Tree */ + if (nodePtr == NULL) + return 0; + + + int leftHeight = getHeight(nodePtr->lchild); + int rightHeight = getHeight(nodePtr->rchild); + + int leftDiameter = getDiameter(nodePtr->lchild); + int rightDiameter = getDiameter(nodePtr->rchild); + return getMax(leftHeight + rightHeight + 1, + getMax(leftDiameter, rightDiameter)); +} + +main() +{int i,n,m; + char s[20]; + scanf("%d %d",&n,&m); + root=getnode(m); + for(i=1;ilchild==NULL) + ptr->lchild=getnode(m); + ptr=ptr->lchild;} + else + { + if(ptr->rchild==NULL) + ptr->rchild=getnode(m); + ptr=ptr->rchild; + } + j++; + } + + } + printf("%d\n",getDiameter(root)); +} diff --git a/Trees/Heap/solution.c b/Trees/Heap/solution.c new file mode 100644 index 0000000..3ae12a0 --- /dev/null +++ b/Trees/Heap/solution.c @@ -0,0 +1,29 @@ +#include +main() +{ + int N,A[100],i,type,q,a,b,j; + scanf("%d",&N); + for(i=0;i0) + { + scanf("%d",&a); + if(a==1) + { scanf("%d",&b); + for(j=0;jlarge) + large=A[j]; + } + printf("%d\n",large); + } + q--; + } +} diff --git a/checker.sh b/checker.sh new file mode 100644 index 0000000..e4a2a70 --- /dev/null +++ b/checker.sh @@ -0,0 +1,66 @@ +#!/bin/sh +# Copyright 2018, Training Cell MEC + +CC=cc +VERSION="0.1" + +PROJ_PATH=$(pwd) +SOL_FILE="solution.c" +TESTCASE_DIR="tests" +OUTPUT_DIR=output + +HELP_TEXT="Usage: $0 [-c CC ] [-d PATH] [-h] PROBLEM" + +PROBLEM= + +help() { + echo "$HELP_TEXT" + exit 0 +} + +error_exit() { + echo "critical error: $1" + exit 1 +} + +while [ $# != 0 ]; do + case $1 in + -h) help ;; + -c) CC=$2 ; shift ;; + -d) PROJ_PATH=$2 ; shift ;; + -*) ;; + *) PROBLEM=$1 ;; + esac + shift +done + +[ -z "$PROBLEM" ] && error_exit "no problem specified" + +# scratch directory +if [ ! -d "${PROJ_PATH}/${OUTPUT_DIR}" ]; then + mkdir "${PROJ_PATH}/${OUTPUT_DIR}" || + error_exit "directory creation failed" +fi +find "${PROJ_PATH}/${OUTPUT_DIR}/" -maxdepth 1 -type f -delete || + error_exit "directory operation failed" + +# build the source +$CC -o "${PROJ_PATH}/${OUTPUT_DIR}/bin" "${PROJ_PATH}/${PROBLEM}/${SOL_FILE}" || + error_exit "compilation failed" + +# verify the output +for cs in $(find "${PROJ_PATH}/${PROBLEM}/${TESTCASE_DIR}/"\ + -maxdepth 1 -iname 'in*.txt' -type f -printf '%f '); do + op_n=$(echo $cs | sed 's/in\(.\)\.txt/\1/') # n as in nth test case + + "${PROJ_PATH}/${OUTPUT_DIR}/bin" <"${PROJ_PATH}/${PROBLEM}/${TESTCASE_DIR}/in${op_n}.txt"\ + >"${PROJ_PATH}/${OUTPUT_DIR}/op${op_n}.txt" + + cat "${PROJ_PATH}/${OUTPUT_DIR}/op${op_n}.txt" | sha1sum | cut -d ' ' -f 1 | + cmp -s -n 40 "${PROJ_PATH}/${PROBLEM}/${TESTCASE_DIR}/op${op_n}_hashed.txt" - && + echo "test case ${op_n} passed" || echo "test case ${op_n} failed" +done + +# cleanup +rm -r "${PROJ_PATH}/${OUTPUT_DIR}/" || + error_exit "directory operation failed"