Post Transition || Hacker Rank

 Question 24 || Hacker Rank

Post Transition || Hacker Rank

Post Transition

We live in a major country. This nation has towns_count in it. Every town has some mail centers in which bundles are put away and moved.

 

Mail depots have different internal design. In particular, every one of them has a few limits on the bundles it can store - their weight ought to be between min_weight and max_weight comprehensively, where min_weight and max_weight are fixed for every office.

 

Bundles are put away in some request in the workplace line. That implies, that they are handled utilizing this request while sending and getting.

 

Once in a while two mail centers, even in various towns, may sort out the accompanying exchange: the first sends every one of its bundles to the subsequent one. The subsequent one acknowledges the bundles that fulfill the weight condition for the subsequent office and rejects any remaining ones. These dismissed bundles return to the main office back and are put away in similar request they were put away before they were sent. The acknowledged bundles move to the tail of the subsequent office's line in similar request they were put away in the primary office.

 

You ought to deal with a few questions in your program. You'll be furnished with structures package, post_office and town. to get done with this responsibility, you ought to fill the accompanying capabilities:

 

Print_all_packages - given the town t, print all bundles around here. They ought to be printed as follows:

 

 

 

Town_name:

    0:

        id_0

        id_1

        ...

    1:

        id_2

        id_3

        ...

    ...

where 0, 1 and so on are the quantities of mailing stations and id0, id1 ... are the ids of bundles from the 0th mail center in the request for its line id2, id3, are from the 1st one and so on. There ought to be one '\t' image before mail center numbers and two '\t' images before the ids.

 

send_all_acceptable_packages - given the towns source and target mailing station records source_office_index and target_office_index, deal with the exchange depicted above between the mail center #source_office_index around source and the #target_office_index mailing station #town_with_most_packages around .

 

find_town - considering all towns, track down the one with the most number of bundles in all mail centers through and through name. In the event that there are a few of them, track down the first from the assortment .

 

 

 

Input Arrangement

 

First line of the information contains a solitary towns_count.towns_count number . blocks follow, each portraying a town.

Each town block contains a few lines. On the primary line there is a string town_name - the name of the town. On the second line there is a whole number offices_count - the quantity of the workplaces in the town. offices_count blocks follow then, at that point, each depicting an office.

 

Each office block likewise contains a few lines. On the principal line there are three numbers isolated by single spaces: (the quantity of bundles in the workplace), and (portrayed previously). blocks follow, each portraying a bundle.

 

Each bundle block contains precisely two lines. On the main line there is a string which is an id of the bundle. On the second line there is a number which is the heaviness of the bundle.

 

Then, there is a solitary number on the line which is the quantity of inquiries. blocks follow, each depicting an inquiry.

 

Each inquiry block contains a few lines. On the main line there is a whole number , or . In the event that this number is , on the second line there is a string - the name of town for which all bundles ought to be printed. In the event that this number is , on the second line there are string , number , string and whole number isolated by single spaces. That implies exchanges between mail center # in the town and mail center # in the town ought to be handled.

 

On the off chance that the number is , no lines follow and the town with the most number of bundles ought to be found.

 

Requirements

Post Transition


 

 

Test Info 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


Test Result 0

 

Town with the most number of bundles is B

Town with the most number of bundles is A

A:

    0:

        a

        b

    1:

        c

        e

        f

        h

B:

    0:

        d


Clarification 0

 

Before all questions, town B has 4 bundles altogether, town A has 3 . In any case, after exchange all bundles from B's th mail center go to the st mailing station of A, with the exception of bundle d since it's excessively light.



Source Code:

#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;
   int packages_count;
};
typedef struct post_office post_office;
struct town
{
   char* name;
   post_office* offices;
   int offices_count;
};
typedef struct town town;
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);
        for (int j = 0; j < t.offices[i].packages_count; j++)
            printf("\t\t%s\n", t.offices[i].packages[j].id);
    }
}
void send_all_acceptable_packages(town* source, int source_office_index, town* target, int target_office_index)
{
    int n = 0;
    for (int i = 0; i < source->offices[source_office_index].packages_count; i++)
        if (source->offices[source_office_index].packages[i].weight >= target->offices[target_office_index].min_weight &&
            source->offices[source_office_index].packages[i].weight <= target->offices[target_office_index].max_weight)
            ++n;
    package* newPackages = malloc(sizeof(package)*(n + target->offices[target_office_index].packages_count));
    package* oldPackages = malloc(sizeof(package)*(source->offices[source_office_index].packages_count - n));
    for (int i = 0; i < target->offices[target_office_index].packages_count; i++)
        newPackages[i] = target->offices[target_office_index].packages[i];
    n = target->offices[target_office_index].packages_count;
    int m = 0;
    for (int i = 0; i < source->offices[source_office_index].packages_count; i++)
        if (source->offices[source_office_index].packages[i].weight >= target->offices[target_office_index].min_weight &&
            source->offices[source_office_index].packages[i].weight <= target->offices[target_office_index].max_weight)
        {
            newPackages[n] = source->offices[source_office_index].packages[i];
            ++n;
        }
        else
        {
            oldPackages[m] = source->offices[source_office_index].packages[i];
            ++m;
        }
    target->offices[target_office_index].packages_count = n;
    free(target->offices[target_office_index].packages);
    target->offices[target_office_index].packages = newPackages;
    source->offices[source_office_index].packages_count = m;
    free(source->offices[source_office_index].packages);
    source->offices[source_office_index].packages = oldPackages;
}
int number_of_packages(town t)
{
    int ans = 0;
    for (int i = 0; i < t.offices_count; i++)
        ans += t.offices[i].packages_count;
    return ans;
}
town town_with_most_packages(town* towns, int towns_count)
{
    int ans;
    int max_packages = -1;
    for (int i = 0; i < towns_count; i++)
        if (number_of_packages(towns[i]) > max_packages)
        {
            max_packages = number_of_packages(towns[i]);
            ans = i;
        }
    return towns[ans];
}
town* find_town(town* towns, int towns_count, char* name)
{
    for (int i = 0; i < towns_count; i++)
        if (!strcmp(towns[i].name, name))
            return &(towns[i]);
    return &towns[0];
}
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 = malloc(sizeof(post_office)*towns[i].offices_count);
      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);
         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);
         }
      }
   }
   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;
}



Output:

Post Transition


Post a Comment

0 Comments