Skip to content

Commit

Permalink
added Hackerrank sql , c ,python
Browse files Browse the repository at this point in the history
  • Loading branch information
arwazkhan189 authored Oct 1, 2021
1 parent 97ec7a7 commit 69f163c
Show file tree
Hide file tree
Showing 83 changed files with 1,270 additions and 0 deletions.
18 changes: 18 additions & 0 deletions HackerRank-C-main/1D Arrays in C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned int n;
scanf("%u",&n);
unsigned int a[n],sum=0;
for (int i=0; i<n; i++) {
scanf("%u",&a[i]);
sum+=a[i];
}
printf("%u",sum);
return 0;
}
25 changes: 25 additions & 0 deletions HackerRank-C-main/Array Reversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}


/* Write the logic to reverse the array. */
int temp=0;
for(int i=0,j=num-1;i<=j;i++,j--){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

for(i = 0; i < num; i++)
printf("%d ", *(arr + i));
return 0;
}
33 changes: 33 additions & 0 deletions HackerRank-C-main/Bitwise Operators.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.


void calculate_the_maximum(int n, int k) {
int AND=0,OR=0,XOR=0;
for (int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if (((i&j) > AND) && ((i&j) < k)) {
AND = i&j;
}
if (((i|j) > OR) && ((i|j) < k)) {
OR = i|j;
}
if (((i^j) > XOR) && ((i^j) < k)) {
XOR = i^j;
}
}
}
printf("%d\n%d\n%d\n",AND,OR,XOR);
}

int main() {
int n, k;

scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);

return 0;
}
40 changes: 40 additions & 0 deletions HackerRank-C-main/Boxes through a Tunnel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41

struct box
{
/* Define three fields of type int: length, width and height*/
int length, width, height;
};

typedef struct box box;

int get_volume(box b) {
/*Return the volume of the box*/
return (b.length)*(b.width)*(b.height); //volume=length*width*height
}

int is_lower_than_max_height(box b) {
/* Return 1 if the box's height is lower than MAX_HEIGHT and 0 otherwise*/
if (b.height<MAX_HEIGHT)
return 1;
else
return 0;
}

int main()
{
int n;
scanf("%d", &n);
box *boxes = malloc(n * sizeof(box));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
}
for (int i = 0; i < n; i++) {
if (is_lower_than_max_height(boxes[i])) {
printf("%d\n", get_volume(boxes[i]));
}
}
return 0;
}
22 changes: 22 additions & 0 deletions HackerRank-C-main/Calculate the Nth term.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.

int find_nth_term(int n, int a, int b, int c) {
//Write your code here.
if (n<=4)
return 6;
return find_nth_term(n-1,a-1,b-2,c-3);
}

int main() {
int n, a, b, c;

scanf("%d %d %d %d", &n, &a, &b, &c);
int ans = find_nth_term(n, a, b, c);

printf("%d", ans);
return 0;
}
77 changes: 77 additions & 0 deletions HackerRank-C-main/Conditional Statements in C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();



int main()
{
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);

if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }

// Write Your Code Here
if (n==1)
printf("one");
else if (n==2)
printf("two");
else if (n==3)
printf("three");
else if (n==4)
printf("four");
else if (n==5)
printf("five");
else if (n==6)
printf("six");
else if (n==7)
printf("seven");
else if (n==8)
printf("eight");
else if (n==9)
printf("nine");
else
printf("Greater than 9");
return 0;
}

char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) { break; }

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

size_t new_length = alloc_length << 1;
data = realloc(data, new_length);

if (!data) { break; }

alloc_length = new_length;
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}

data = realloc(data, data_length);

return data;
}
22 changes: 22 additions & 0 deletions HackerRank-C-main/Digit Frequency.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

char S[1000];
int h[10]={0,0,0,0,0,0,0,0,0,0} ; //hasing technique

scanf("%s",S);

for(int i = 0 ; i <= strlen(S);i++){
if(S[i]>='0' && S[i]<='9')
h[S[i]-'0']++ ;
}

for(int i = 0 ; i <= 9 ;i++)
printf("%d ",h[i]) ;

return 0;
}
68 changes: 68 additions & 0 deletions HackerRank-C-main/Dynamic Array in C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>

int* total_number_of_books;

int** total_number_of_pages;

int main()
{
int total_number_of_shelves;
scanf("%d", &total_number_of_shelves);

int total_number_of_queries;
scanf("%d", &total_number_of_queries);

total_number_of_books=(int*)malloc(sizeof(int)*total_number_of_shelves);

total_number_of_pages=(int**)malloc(sizeof(int*)*total_number_of_shelves);

for(int i=0; i<total_number_of_shelves; i++){
total_number_of_books[i]=0;
total_number_of_pages[i]=(int*)malloc(sizeof(int));
}

while (total_number_of_queries--) {
int type_of_query;
scanf("%d", &type_of_query);

if (type_of_query == 1) {
/*
* Process the query of first type here.
*/
int x, y;
scanf("%d %d", &x, &y);

*(total_number_of_books+x)+=1;

*(total_number_of_pages+x)=realloc(*(total_number_of_pages+x), *(total_number_of_books+x)*sizeof(int));

*(*(total_number_of_pages+x)+*(total_number_of_books+x)-1)=y;

} else if (type_of_query == 2) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", *(*(total_number_of_pages + x) + y));
} else {
int x;
scanf("%d", &x);
printf("%d\n", *(total_number_of_books + x));
}
}

if (total_number_of_books) {
free(total_number_of_books);
}

for (int i = 0; i < total_number_of_shelves; i++) {
if (*(total_number_of_pages + i)) {
free(*(total_number_of_pages + i));
}
}

if (total_number_of_pages) {
free(total_number_of_pages);
}

return 0;
}
19 changes: 19 additions & 0 deletions HackerRank-C-main/For Loop in C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>



int main()
{
int a, b;
scanf("%d\n%d", &a, &b);
// Complete the code.
char c[10][10]={"","one","two","three","four","five","six","seven","eight","nine"};
for (int n=a; n<=b; n++) {
((n<=9)?printf("%s\n",c[n]):(n%2==0)?printf("even\n"):printf("odd\n"));
}
return 0;
}

16 changes: 16 additions & 0 deletions HackerRank-C-main/Functions in C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/
int max_of_four(int w,int x,int y,int z){
return ((w>x && w>y && w>z)?w:(x>w && x>y && x>z)?x:(y>w && y>x && y>z)?y:z);
}

int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);

return 0;
}
19 changes: 19 additions & 0 deletions HackerRank-C-main/Playing With Characters.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char ch,s[100],sen[100];
scanf("%c",&ch);
scanf("%s",s);
scanf(" %[^\n]s",sen);
printf("%c\n",ch);
printf("%s\n",s);
printf("%s\n",sen);

return 0;
}
23 changes: 23 additions & 0 deletions HackerRank-C-main/Pointers in C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

void update(int *a,int *b) {
// Complete this function
int bb;
bb = *a;
*a=*a+*b;
if (bb>*b)
*b=bb-*b;
else
*b=*b-bb;
}

int main() {
int a, b;
int *pa = &a, *pb = &b;

scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);

return 0;
}
Loading

0 comments on commit 69f163c

Please sign in to comment.