Skip to content

Commit 68db998

Browse files
authored
More additions from practice work, pardon the mess.
1 parent 371f336 commit 68db998

30 files changed

+2157
-95
lines changed

Codechef.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stdio.h>
2+
void main(){
3+
int a,b,c,d,x=0,y=0,count=0,arayA[10],arayB[10];
4+
scanf("%d %d %d %d",&a,&b,&c,&d);
5+
int i=a,j=c;
6+
while(i<=b && j<=d){
7+
arayA[x++]=i;
8+
arayB[y++]=j;
9+
i++;
10+
j++;
11+
}
12+
for(i=0;i<x;i++){
13+
for(j=0;j<y;j++){
14+
if(arayA[j]<arayB[i]){
15+
count++;
16+
}
17+
}
18+
}
19+
printf("%d",count);
20+
}

HashT.c

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include<stdio.h>
2+
#include<malloc.h>
3+
#include<stdlib.h>
4+
#define LOAD_FACTOR 20
5+
6+
struct ListNode{
7+
int key;
8+
int data;
9+
struct ListNode *next;
10+
};
11+
12+
struct HashTableNode{
13+
int bcount;
14+
struct ListNode *next;
15+
};
16+
17+
struct HashTable{
18+
int tsize;
19+
int count;
20+
struct HashTableNode **Table;
21+
};
22+
23+
struct HashTable *CreateHashTable(int size){
24+
int i;
25+
struct HashTable *h;
26+
h = (struct HashTable *)malloc(sizeof(struct HashTable));
27+
if(!h){
28+
return NULL;
29+
}
30+
31+
h->tsize = size/LOAD_FACTOR;
32+
h->count = 0;
33+
h->Table = (struct HashTableNode **)malooc(sizeof(struct HashTableNode *) *h->tsize);
34+
35+
if(!h->Table){
36+
printf("Memory Error");
37+
return NULL;
38+
}
39+
40+
for(i=0;i<h->tsize;i++){
41+
h->Table[i]->next = NULL;
42+
h->Table[i]->bcount = 0;
43+
}
44+
return h;
45+
}
46+
47+
int HashSearch(struct HashTable *h, int data){
48+
struct ListNode *temp;
49+
temp = h->Table[Hash(key,h->tsize)]->next;
50+
while(temp){
51+
if(temp->data==data)
52+
return 1;
53+
temp = temp->next;
54+
}
55+
return 0;
56+
}
57+
58+
int HashInsert(struct HashTable *h, int data){
59+
int index;
60+
struct ListNode *temp, *newNode;
61+
62+
if(HashSearch(h,data)){
63+
return 0;
64+
}
65+
66+
index = Hash(key,h->tsize);
67+
temp = h->Table[index]->next;
68+
newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
69+
if(!newNode){
70+
printf("Out Of Space");
71+
return -1;
72+
}
73+
74+
newNode->key = index;
75+
newNode->data = data;
76+
newNode->next = h->Table[index]->next;
77+
h->Table[index]->next = newNode;
78+
79+
h->Table[index]->bcount++;
80+
h->count++;
81+
if(h->count/h->tsize > LOAD_FACTOR){
82+
Rehash(h);
83+
return 1;
84+
}
85+
}
86+
87+
int HashDelete(struct HashTable *h, int data){
88+
int index;
89+
struct ListNode *temp;
90+
index =Hash(data,h->tsize);
91+
92+
for(temp=h->Table[index]->next,prev=NULL;temp;prev=temp,temp=temp->next){
93+
if(temp->data==data){
94+
if(prev!=NULL){
95+
prev->next = temp->next;
96+
}
97+
free(temp);
98+
h->Table[index]->bcount--;
99+
h->count--;
100+
return 1;
101+
}
102+
}
103+
return 0;
104+
}
105+
106+
void Rehash(Struct HashTable *h){
107+
int oldsize,i,index;
108+
struct ListNode *p, *temp;
109+
struct HashTableNode **oldTable;
110+
oldsize = h->tsize;
111+
oldTable = h->Table;
112+
h->tsize = h->tsize*2;
113+
114+
h->Table= (struct HashTableNode **)malloc(h->tsize*sizeof(struct HashTableNode *));
115+
if(!h->Table){
116+
printf("Allocation Failed");
117+
return;
118+
}
119+
for(i=0;i<oldsize;i++){
120+
for(temp=oldTable[i]->next;temp;temp=temp->next){
121+
index = Hash(temp->data,h->tsize);
122+
temp->next = h->Table[index]->next;
123+
h->Table[index]->next = temp;
124+
}
125+
}
126+
}

India.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
int main()
3+
{
4+
int a = 10, b = 0, c = 10;
5+
char* str = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq "
6+
"TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL"
7+
"OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm "
8+
"SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
9+
while (a != 0)
10+
{
11+
a = str[b++];
12+
while (a-- > 6
13+
4)
14+
{
15+
if (++c == 90)
16+
{
17+
c = 10;
18+
putchar('\n');
19+
}
20+
else
21+
{
22+
if (b % 2 == 0)
23+
putchar('!');
24+
else
25+
putchar(' ');
26+
}
27+
}
28+
}
29+
return 0;
30+
}

LOD.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<stdio.h>
2+
3+
void main(){
4+
5+
struct Queue *Q;
6+
struct Stack *s = CreateStack();
7+
struct BinaryTree *temp;
8+
if(!root){
9+
return;
10+
}
11+
Q = CreateQueue();
12+
EnQueue(Q,root);
13+
14+
while(!IsEmptyQueue(Q)){
15+
temp = Q->front;
16+
17+
if(temp->right){
18+
EnQueue(Q,temp->right);
19+
}
20+
if(temp->left){
21+
EnQueue(Q,temp->left);
22+
}
23+
24+
temp=DeQueue(Q);
25+
Push(s,temp);
26+
}
27+
28+
while(!IsEmptyStack(s)){
29+
printf("%d",Pop(s)->data);
30+
}
31+
}

NimGame.c

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
void main(){
5+
6+
//Game Starts Here!!
7+
printf("Welcome to NimGame\n\n");
8+
9+
//Intialising useful Variables
10+
int C=0,Sticks,u,TBlanks,Blanks,Counter,i,x,p,j,NoBlank,Start,End;
11+
12+
//Initialising and Declaring useful Variables
13+
char Input[5],Array[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
14+
15+
//This Piece of code(Z) Displays the Moves
16+
Z:
17+
18+
//Declaring useful Variables
19+
Sticks=3,Blanks=0,Counter=0,NoBlank=00;
20+
21+
//Start of Pattern
22+
printf("\t + + + + + + + + +\n");
23+
24+
while(Sticks<=7){
25+
26+
printf("\t + ");
27+
28+
for(i=1;i<=Sticks;i++){
29+
if(Array[Counter]==1)
30+
{
31+
printf("| ");
32+
}else{
33+
printf(" ");
34+
}
35+
++Counter;
36+
}
37+
38+
for(j=1;j<=(7-Sticks);j++){
39+
printf(" ");
40+
}
41+
printf("+");
42+
43+
printf("\n");
44+
45+
Sticks+=2;
46+
}
47+
48+
printf("\t + + + + + + + + +\n");
49+
//End of Pattern
50+
51+
//=========+=======+=======+=======+=======+=========+=============+===============++==============+============+=========+
52+
printf("%d",C);
53+
//Asking Apt. User for Input
54+
if((C%2)!=0){
55+
printf("Player 1 Enter The Range to be Deleted <Start-End><Start=End, for Single Deletion> : ");
56+
}else{
57+
printf("Player 2 Enter The Range to be Deleted <Start-End><Start=End, for Single Deletion> : ");
58+
}
59+
60+
//Input from User
61+
62+
scanf("%s",&Input);
63+
64+
printf("\n");
65+
66+
//Extracting Start of Range
67+
Start=(((Input[0]-48)*10)+(Input[1]-48));
68+
69+
//Extracting End of Range
70+
End=(((Input[3]-48)*10)+(Input[4]-48));
71+
//printf("%d%d",Start,End);
72+
//Loop to count No. Of Blanks b/w User's Start and End Position(s)
73+
for(x=Start;x<=End;x++)
74+
{
75+
if(Array[(x-1)]==0){Blanks++;}
76+
77+
}//printf("%d",Blanks);
78+
79+
//Validating the Move
80+
if(Blanks==0){
81+
NoBlank=11;
82+
}
83+
else{
84+
NoBlank=00;
85+
}
86+
87+
//Counting No. Of Blanks in Nimgame
88+
for(u=0;u<15;u++)
89+
{
90+
if(Array[u]==0){TBlanks++;}
91+
}
92+
93+
//Checks for Valid Move i.e without Blanks
94+
if(NoBlank==11){
95+
96+
//Checks for Valid Move i.e Same Row is Valid else not
97+
if(((Start<=3)&&(End>=4))||((Start<=8)&&(End>=9)))
98+
{
99+
printf("Invalid Move!! Choose From Single Row...\n\n");
100+
goto Z;
101+
102+
}else{
103+
104+
105+
if(TBlanks<15){
106+
if((C%2)!=0){
107+
printf("\n\tPlayer 1's Move:%s\n",Input);
108+
109+
//Makes Blanks in ARRAY as per Valid Input
110+
for(p=Start;p<=End;p++){
111+
Array[(p-1)]=0;
112+
}
113+
114+
}else{
115+
printf("\n\tPlayer 2's Move:%s\n",Input);
116+
//Makes Blanks in ARRAY as per Valid Input
117+
for(p=Start;p<=End;p++){
118+
Array[(p-1)]=0;
119+
}
120+
}
121+
//Counter of Chance
122+
++C;
123+
goto Z;
124+
125+
}else{
126+
if((C%2)!=0){
127+
printf("\n\tPlayer 2 Won!! :)\n");
128+
}else{
129+
printf("\n\tPlayer 1 Won!! :)\n");
130+
}
131+
}
132+
133+
}
134+
135+
}else{
136+
printf("Blanks Cannot be Deleted\n\n");
137+
goto Z;
138+
}
139+
140+
//End Of Game
141+
}

Permutation.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<stdio.h>
2+
#include<iostream>
3+
#include<stdlib.h>
4+
using namespace std;
5+
6+
void Perm(int a[], int k, int n){
7+
if(k==n){
8+
for(int i=1; i<=n; i++){
9+
cout<<a[i]<<" ";
10+
cout<<endl;
11+
}
12+
}else{
13+
for(int i=k; i<=n; i++){
14+
int t=a[k];
15+
a[k]=a[i];
16+
a[i]=t;
17+
Perm(a,k+1,n);
18+
t=a[k];
19+
a[k]=a[i];
20+
a[i]=t;
21+
}
22+
}
23+
}
24+
25+
int main(){
26+
int a[10]={1,2,3,4,5,6,7,8,9,10};
27+
Perm(a,5,7);
28+
}

0 commit comments

Comments
 (0)