Skip to content

Commit 5868198

Browse files
2 parents 7a88f90 + 35208e3 commit 5868198

File tree

7 files changed

+293
-3
lines changed

7 files changed

+293
-3
lines changed

Hackerrank/3-Arrays_and_Strings/5-Dynamic_Array_in_C.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@ int main(){
1414
int total_number_of_queries;
1515
scanf("%d", &total_number_of_queries);
1616

17+
// !
18+
total_number_of_books=(int*)malloc(sizeof(int)*total_number_of_shelves);
19+
20+
total_number_of_pages=(int**)malloc(sizeof(int*)*total_number_of_shelves);
21+
22+
for(int i=0; i<total_number_of_shelves; i++){
23+
total_number_of_books[i]=0;
24+
total_number_of_pages[i]=(int*)malloc(sizeof(int));
25+
}
26+
// !
27+
1728
while (total_number_of_queries--) {
1829
int type_of_query;
1930
scanf("%d", &type_of_query);
2031

2132
if (type_of_query == 1) {
2233
int x, y;
2334
scanf("%d %d", &x, &y);
35+
2436
// !
25-
*(total_number_of_books + x) = (int *)realloc(total_number_of_books+x, )
26-
*(total_number_of_books + x) += 1;
27-
*(*(total_number_of_pages + x)) += y;
37+
*(total_number_of_books+x)+=1;
38+
*(total_number_of_pages+x)=realloc(*(total_number_of_pages+x), *(total_number_of_books+x)*sizeof(int));
39+
*(*(total_number_of_pages+x)+*(total_number_of_books+x)-1)=y;
2840
// !
2941

3042
} else if (type_of_query == 2) {
Binary file not shown.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
int lexicographic_sort(const char* a, const char* b) {
5+
strcmp(a,b);
6+
}
7+
8+
int lexicographic_sort_reverse(const char* a, const char* b) {
9+
strcmp(b,a);
10+
}
11+
12+
int characters_count(const char *s)
13+
{
14+
int n = 0;
15+
int count[128] = {0};
16+
if (NULL == s)
17+
{
18+
return -1;
19+
}
20+
while(*s != '\0')
21+
{
22+
if (!count[*s])
23+
{
24+
count[*s]++;
25+
n++;
26+
}
27+
s++;
28+
}
29+
return n;
30+
}
31+
32+
int sort_by_number_of_distinct_characters(const char* a, const char* b)
33+
{
34+
int con = characters_count(a) - characters_count(b);
35+
return (con ? con : lexicographic_sort(a, b));
36+
}
37+
38+
int sort_by_length(const char* a, const char* b) {
39+
if(strlen(a) > strlen(b))
40+
return 1;
41+
else if(strlen(a) == strlen(b))
42+
lexicographic_sort(a,b);
43+
else
44+
return 0;
45+
}
46+
47+
void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){
48+
int i,x=0;
49+
50+
while(x != 1){
51+
x=1;
52+
for(i=0; i<len; i++){
53+
if(cmp_func(arr[i], arr[i+1]) > 0){
54+
char *temp= arr[i];
55+
arr[i]= arr[i+1];
56+
arr[i+1]= temp;
57+
x= 0;
58+
}
59+
}
60+
}
61+
}
62+
63+
64+
int main()
65+
{
66+
int n;
67+
scanf("%d", &n);
68+
69+
char** arr;
70+
arr = (char**)malloc(n * sizeof(char*));
71+
72+
for(int i = 0; i < n; i++){
73+
*(arr + i) = malloc(1024 * sizeof(char));
74+
scanf("%s", *(arr + i));
75+
*(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);
76+
}
77+
78+
string_sort(arr, n, lexicographic_sort);
79+
for(int i = 0; i < n; i++)
80+
printf("%s\n", arr[i]);
81+
printf("\n");
82+
83+
string_sort(arr, n, lexicographic_sort_reverse);
84+
for(int i = 0; i < n; i++)
85+
printf("%s\n", arr[i]);
86+
printf("\n");
87+
88+
string_sort(arr, n, sort_by_length);
89+
for(int i = 0; i < n; i++)
90+
printf("%s\n", arr[i]);
91+
printf("\n");
92+
93+
string_sort(arr, n, sort_by_number_of_distinct_characters);
94+
for(int i = 0; i < n; i++)
95+
printf("%s\n", arr[i]);
96+
printf("\n");
97+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX_HEIGHT 41
4+
5+
struct box
6+
{
7+
int length; int width; int height;
8+
};
9+
10+
typedef struct box box;
11+
12+
int get_volume(box b) {
13+
return b.height*b.length*b.width;
14+
}
15+
16+
int is_lower_than_max_height(box b) {
17+
if(b.height >= MAX_HEIGHT){
18+
return 0;
19+
}
20+
return 1;
21+
}
22+
23+
int main()
24+
{
25+
int n;
26+
scanf("%d", &n);
27+
box *boxes = malloc(n * sizeof(box));
28+
for (int i = 0; i < n; i++) {
29+
scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
30+
}
31+
for (int i = 0; i < n; i++) {
32+
if (is_lower_than_max_height(boxes[i])) {
33+
printf("%d\n", get_volume(boxes[i]));
34+
}
35+
}
36+
return 0;
37+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <math.h>
4+
5+
struct triangle
6+
{
7+
int a;
8+
int b;
9+
int c;
10+
};
11+
12+
typedef struct triangle triangle;
13+
void sort_by_area(triangle* tr, int n) {
14+
15+
triangle Ttemp;
16+
int j, i, arrN= (int *)malloc(n * sizeof(int));
17+
double p, S, Dtemp, *arrS= (double *)malloc(n * sizeof(double));
18+
for(i=0; i<n; i++){
19+
p= ((double)tr[i].a + (double)tr[i].b + (double)tr[i].c)/2;
20+
// printf("**%lf**\n",p);
21+
S= sqrt(p*(p- (double)tr[i].a)*(p- (double)tr[i].b)*(p- (double)tr[i].c));
22+
arrS[i]= S;
23+
// printf("**%lf**\n",arrS[i]);
24+
}
25+
for(i=0; i<n; i++){
26+
for(j=1; j<n; j++){
27+
if(arrS[j-1] > arrS[j]){
28+
Dtemp= arrS[j-1]; Ttemp= tr[j-1];
29+
arrS[j-1]= arrS[j]; tr[j-1]= tr[j];
30+
arrS[j]= Dtemp; tr[j]= Ttemp;
31+
}
32+
}
33+
}
34+
}
35+
36+
int main()
37+
{
38+
int n;
39+
scanf("%d", &n);
40+
triangle *tr = malloc(n * sizeof(triangle));
41+
for (int i = 0; i < n; i++) {
42+
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
43+
}
44+
sort_by_area(tr, n);
45+
for (int i = 0; i < n; i++) {
46+
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
47+
}
48+
return 0;
49+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX_STRING_LENGTH 6
4+
5+
struct package
6+
{
7+
char* id;
8+
int weight;
9+
};
10+
11+
typedef struct package package;
12+
13+
struct post_office
14+
{
15+
int min_weight;
16+
int max_weight;
17+
package* packages;
18+
int packages_count;
19+
};
20+
21+
typedef struct post_office post_office;
22+
23+
struct town
24+
{
25+
char* name;
26+
post_office* offices;
27+
int offices_count;
28+
};
29+
30+
typedef struct town town;
31+
32+
33+
34+
void print_all_packages(town t) {
35+
}
36+
37+
void send_all_acceptable_packages(town* source, int source_office_index, town* target, int target_office_index) {
38+
}
39+
40+
town town_with_most_packages(town* towns, int towns_count) {
41+
}
42+
43+
town* find_town(town* towns, int towns_count, char* name) {
44+
}
45+
46+
int main()
47+
{
48+
int towns_count;
49+
scanf("%d", &towns_count);
50+
town* towns = malloc(sizeof(town)*towns_count);
51+
for (int i = 0; i < towns_count; i++) {
52+
towns[i].name = malloc(sizeof(char) * MAX_STRING_LENGTH);
53+
scanf("%s", towns[i].name);
54+
scanf("%d", &towns[i].offices_count);
55+
towns[i].offices = malloc(sizeof(post_office)*towns[i].offices_count);
56+
for (int j = 0; j < towns[i].offices_count; j++) {
57+
scanf("%d%d%d", &towns[i].offices[j].packages_count, &towns[i].offices[j].min_weight, &towns[i].offices[j].max_weight);
58+
towns[i].offices[j].packages = malloc(sizeof(package)*towns[i].offices[j].packages_count);
59+
for (int k = 0; k < towns[i].offices[j].packages_count; k++) {
60+
towns[i].offices[j].packages[k].id = malloc(sizeof(char) * MAX_STRING_LENGTH);
61+
scanf("%s", towns[i].offices[j].packages[k].id);
62+
scanf("%d", &towns[i].offices[j].packages[k].weight);
63+
}
64+
}
65+
}
66+
int queries;
67+
scanf("%d", &queries);
68+
char town_name[MAX_STRING_LENGTH];
69+
while (queries--) {
70+
int type;
71+
scanf("%d", &type);
72+
switch (type) {
73+
case 1:
74+
scanf("%s", town_name);
75+
town* t = find_town(towns, towns_count, town_name);
76+
print_all_packages(*t);
77+
break;
78+
case 2:
79+
scanf("%s", town_name);
80+
town* source = find_town(towns, towns_count, town_name);
81+
int source_index;
82+
scanf("%d", &source_index);
83+
scanf("%s", town_name);
84+
town* target = find_town(towns, towns_count, town_name);
85+
int target_index;
86+
scanf("%d", &target_index);
87+
send_all_acceptable_packages(source, source_index, target, target_index);
88+
break;
89+
case 3:
90+
printf("Town with the most number of packages is %s\n", town_with_most_packages(towns, towns_count).name);
91+
break;
92+
}
93+
}
94+
return 0;
95+
}

0 commit comments

Comments
 (0)