## Algorithm

Problem Name: Post Transition

In this HackerRank Functions in C programming problem solution,

We live in a big country. This country has towns_count towns in it. Each town has some post offices in which packages are stored and transferred. Post offices have different inner structure. Specifically, each of them has some limitations on the packages it can store - their weight should be between min_weight and max_weight inclusively, where min_weight and max_weight are fixed for each office.

Packages are stored in some order in the office queue. That means, that they are processed using this order when sending and receiving.

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.

You should process several queries in your program. You'll be provided with structures package,post_office and town . in order to complete this task, you should fill the following functions:

print_all_packages - given the town t , print all packages in this town. They should be printed as follows:

Town_name:
0:
id_0
id_1
...
1:
id_2
id_3
...
...


where 0,1 etc are the numbers of post offices and id0,id1 ... are the ids of packages from the 0 th post office in the order of its queue, id2,id3 are from the 1 stone etc. There should be one '\t' symbol before post office numbers and two '\t' symbols before the ids.

Output Format

For queries of type

, print all packages in the format provided in the problem statement. For queries of type , print "Town with the most number of packages is

" on a separate line.

Sample Input 0

2
A
2
2 1 3
a 2
b 3
1 2 4
c 2
B
1
4 1 4
d 1
e 2
f 3
h 4
5
3
2 B 0 A 1
3
1 A
1 B


Sample Output 0

Town with the most number of packages is B
Town with the most number of packages is A
A:
0:
a
b
1:
c
e
f
h
B:
0:
d


## Code Examples

### #1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LENGTH 6

struct package
{
char* id;
int weight;
};
typedef struct package package;

struct post_office
{
int min_weight;
int max_weight;
//package* packages;    //Removed
int packages_count;
};
typedef struct post_office post_office;

struct town
{
char* name;
post_office* offices;
int offices_count;
};
typedef struct town town;

office -> packagesTail = office -> packagesHead;
}
else{
office->packagesTail -> next = (package*)malloc(sizeof(package));
office->packagesTail = office->packagesTail->next;
}
office -> packagesTail -> next = NULL;
office -> packagesTail -> id = malloc(sizeof(char) * MAX_STRING_LENGTH);
scanf("%s", office -> packagesTail -> id);
scanf("%d", &office -> packagesTail -> weight);
return;
}

*Tail = node;
}
else{
(*Tail)->next = node;
*Tail = node;
}
}

if((*Head) == prev && prev == current)
else if((*Tail) == current)
(*Tail) = prev;
prev -> next =current -> next;
}

void print_all_packages(town t) {
printf("%s:\n",t.name);
for(int i=0; i<t.offices_count; i++){
printf("\t%d:\n",i);
while(node! = NULL>{
printf("\t\t%s\n",node->id);
node = node -> next;
}
}
}

void send_all_acceptable_packages(town* source, int source_office_index, town* target, int target_office_index) {
post_office* sourceOffice = source -> offices+source_office_index;
post_office* targetOffice = target -> offices+target_office_index;

package* travelS = sourceOffice -> packagesHead;
package* tempPreviousS = travelS;
package* tempDeleteS = travelS;
package** sourceTail = &sourceOffice -> packagesTail;
package** targetTail = &targetOffice -> packagesTail;

while(travelS! = NULL){
if((travelS -> weight >= targetOffice -> min_weight) && (travelS -> weight  < = targetOffice -> max_weight)){
travelS = travelS->next;
(*targetTail) -> next = NULL;
targetOffice -> packages_count++;
sourceOffice -> packages_count--;
}
else{
tempPreviousS = travelS;
travelS = travelS -> next;
}
}
}

town town_with_most_packages(town* towns, int towns_count) {
int twmpi = 0; //Town With Most Packages Index
int mpnt = 0; //Maximum Packages Number of a Twon
int pnt = 0; //Packages Number of a Town
for(int i=0; i < towns_count; i++,pnt=0){
for(int j=0; j < towns[i].offices_count; j++)
pnt += towns[i].offices[j].packages_count;
if(pnt>mpnt){
mpnt = pnt;
twmpi = i;
}
}
}

town* find_town(town* towns, int towns_count, char* name) {
int townIndex = 0;
for(int i=0; i < towns_count; i++)
if(strcmp(towns[i].name,name) == 0){
townIndex = i;
break;
}
return &towns[townIndex];
}

int main()
{
int towns_count;
scanf("%d", &towns_count);
town* towns = malloc(sizeof(town)*towns_count);
for (int i = 0; i  <  towns_count; i++) {
towns[i].name = malloc(sizeof(char) * MAX_STRING_LENGTH);
scanf("%s", towns[i].name);
scanf("%d", &towns[i].offices_count);
towns[i].offices = calloc(towns[i].offices_count,sizeof(post_office));
for (int j = 0; j  <  towns[i].offices_count; j++) {
scanf("%d%d%d", &towns[i].offices[j].packages_count, &towns[i].offices[j].min_weight, &towns[i].offices[j].max_weight);
//towns[i].offices[j].packages = malloc(sizeof(package)*towns[i].offices[j].packages_count);    //Removed
for (int k = 0; k  <  towns[i].offices[j].packages_count; k++) {
/*towns[i].offices[j].packages[k].id = malloc(sizeof(char) * MAX_STRING_LENGTH);
scanf("%s", towns[i].offices[j].packages[k].id);
scanf("%d", &towns[i].offices[j].packages[k].weight);*/ //Removed
}
}
}

int queries;
scanf("%d", &queries);
char town_name[MAX_STRING_LENGTH];
while (queries--) {
int type;
scanf("%d", &type);
switch (type) {
case 1:
scanf("%s", town_name);
town* t = find_town(towns, towns_count, town_name);
print_all_packages(*t);
break;
case 2:
scanf("%s", town_name);
town* source = find_town(towns, towns_count, town_name);
int source_index;
scanf("%d", &source_index);
scanf("%s", town_name);
town* target = find_town(towns, towns_count, town_name);
int target_index;
scanf("%d", &target_index);
send_all_acceptable_packages(source, source_index, target, target_index);
break;
case 3:
printf("Town with the most number of packages is %s\n", town_with_most_packages(towns, towns_count).name>;
break;
}
}
return 0;
}

Copy The Code &

Input

cmd
2 A 2 2 1 3 a 2 b 3 1 2 4 c 2 B 1 4 1 4 d 1 e 2 f 3 h 4 5 3 2 B 0 A 1 3 1 A 1 B

Output

cmd
Town with the most number of packages is B Town with the most number of packages is A A: 0: a b 1: c e f h B: 0: d