HackerRank C- Post Transition




Sometimes two post offices, even in different towns, may organize the following transaction: the first one sends all its packages to the second one. The second one accepts the packages that satisfy the weight condition for the second office and rejects all other ones. These rejected packages return to the first office back and are stored in the same order they were stored before they were sent. The accepted packages move to the tail of the second office's queue in the same order they were stored in the first office.



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_STRING_LENGTH 6
  4. struct package
  5. {
  6. char* id;
  7. int weight;
  8. };
  9. typedef struct package package;
  10. struct post_office
  11. {
  12. int min_weight;
  13. int max_weight;
  14. package* packages;
  15. int packages_count;
  16. };
  17. typedef struct post_office post_office;
  18. struct town
  19. {
  20. char* name;
  21. post_office* offices;
  22. int offices_count;
  23. };
  24. typedef struct town town;
  25. void print_all_packages(town t) {
  26. printf("%s:\n", t.name);
  27. for (int i = 0; i < t.offices_count; i++) {
  28. printf("\t%d:\n", i);
  29. for (int j = 0; j < t.offices[i].packages_count; j++) {
  30. printf("\t\t%s\n", t.offices[i].packages[j].id);
  31. }
  32. }
  33. }
  34. int is_movable(post_office po, int w) {
  35. if (w >= po.min_weight && w <= po.max_weight)
  36. return 1;
  37. else
  38. return 0;
  39. }
  40. void send_all_acceptable_packages(town *s, int si, town *t, int ti) {
  41. int i, j;
  42. i = 0;
  43. while (i < s->offices[si].packages_count) {
  44. if (is_movable(t->offices[ti], s->offices[si].packages[i].weight)) {
  45. int *tp = &t->offices[ti].packages_count;
  46. t->offices[ti].packages = (package *) realloc(t->offices[ti].packages, (*tp+1) * sizeof(package));
  47. t->offices[ti].packages[(*tp)++] = s->offices[si].packages[i];
  48. int *sp = &s->offices[si].packages_count;
  49. for (j = i; j < *sp-1; j++) {
  50. s->offices[si].packages[j] = s->offices[si].packages[j+1];
  51. }
  52. s->offices[si].packages = (package *) realloc(s->offices[si].packages, (*sp-1) * sizeof(package));
  53. (*sp)--;
  54. }
  55. else i++;
  56. }
  57. }
  58. town town_with_most_packages(town *towns, int towns_count) {
  59. int max = 0 , r = 0, i;
  60. for (i = 0; i < towns_count; i++) {
  61. int packages_count = 0;
  62. for (int j = 0; j < towns[i].offices_count; j++) {
  63. packages_count += towns[i].offices[j].packages_count;
  64. }
  65. if (packages_count > max) {
  66. max = packages_count;
  67. r = i;
  68. }
  69. }
  70. return towns[r];
  71. }
  72. town *find_town(town *towns, int towns_count, char *name) {
  73. for (int i = 0; i < towns_count; i++) {
  74. if (!strcmp(name, towns[i].name))
  75. return &towns[i];
  76. }
  77. return NULL;
  78. }
  79. int main()
  80. {
  81. int towns_count;
  82. scanf("%d", &towns_count);
  83. town* towns = malloc(sizeof(town)*towns_count);
  84. for (int i = 0; i < towns_count; i++) {
  85. towns[i].name = malloc(sizeof(char) * MAX_STRING_LENGTH);
  86. scanf("%s", towns[i].name);
  87. scanf("%d", &towns[i].offices_count);
  88. towns[i].offices = malloc(sizeof(post_office)*towns[i].offices_count);
  89. for (int j = 0; j < towns[i].offices_count; j++) {
  90. scanf("%d%d%d", &towns[i].offices[j].packages_count, &towns[i].offices[j].min_weight, &towns[i].offices[j].max_weight);
  91. towns[i].offices[j].packages = malloc(sizeof(package)*towns[i].offices[j].packages_count);
  92. for (int k = 0; k < towns[i].offices[j].packages_count; k++) {
  93. towns[i].offices[j].packages[k].id = malloc(sizeof(char) * MAX_STRING_LENGTH);
  94. scanf("%s", towns[i].offices[j].packages[k].id);
  95. scanf("%d", &towns[i].offices[j].packages[k].weight);
  96. }
  97. }
  98. }
  99. int queries;
  100. scanf("%d", &queries);
  101. char town_name[MAX_STRING_LENGTH];
  102. while (queries--) {
  103. int type;
  104. scanf("%d", &type);
  105. switch (type) {
  106. case 1:
  107. scanf("%s", town_name);
  108. town* t = find_town(towns, towns_count, town_name);
  109. print_all_packages(*t);
  110. break;
  111. case 2:
  112. scanf("%s", town_name);
  113. town* source = find_town(towns, towns_count, town_name);
  114. int source_index;
  115. scanf("%d", &source_index);
  116. scanf("%s", town_name);
  117. town* target = find_town(towns, towns_count, town_name);
  118. int target_index;
  119. scanf("%d", &target_index);
  120. send_all_acceptable_packages(source, source_index, target, target_index);
  121. break;
  122. case 3:
  123. printf("Town with the most number of packages is %s\n", town_with_most_packages(towns, towns_count).name);
  124. break;
  125. }
  126. }
  127. return 0;
  128. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube