TopCoder problem "PacketRepack" used in TCO07 Qual 3 (Division I Level One)



Problem Statement

    

You are writing a function to make two pieces of network equipment from different vendors talk to each other. They send and receive data packets with the same data fields, but arranged in opposite orders. Given a data packet from one piece of equipment, reverse the order of the data fields so the second piece of equipment can read it.

The input data will be packed into N b-bit words, for a total of N*b bits. These words will be given to you as a int[] input. The bit 0 of input[0] is bit 0 of the data packet, and bit b-1 of input[N-1] is bit N*b-1 of the data packet. There will be num_fields fields, each field_size bits long. The first field is packed into bits 0 through field_size-1, the second field is packed into bits field_size through field_size*2-1, etc.

For example, given an input packet of { 22, 37, 3 }, with 6-bit words, and four 4-bit fields, the fields would be extracted as shown below:


     input[2]    input[1]    input[0]
         |           |           |
    ----------- ----------- -----------
    0 0 0 0 1 1 1 0 0 1 0 1 0 1 0 1 1 0
        ------- ------- ------- -------
           |       |       |       |
           D       C       B       A

Where A is the first field, B is the second field, C is the third field, and D is the forth field. These fields have the values 6, 5, 9, and 3, respectively. Reversing the order, the fields would be repacked like this:


     output[2]   output[1]   output[0]
         |           |           |
    ----------- ----------- -----------
    0 0 0 1 1 0 0 1 0 1 1 0 0 1 0 0 1 1
        ------- ------- ------- -------
           |       |       |       |
           A       B       C       D

and the correct output is { 19, 22, 6 }.

As shown in the example above, the upper bits of the packet may be unused. These bits will be zero in the input, and must be set to zero in the output as well.

 

Definition

    
Class:PacketRepack
Method:output
Parameters:int[], int, int, int
Returns:int[]
Method signature:int[] output(int[] input, int b, int num_fields, int field_size)
(be sure your method is public)
    
 

Constraints

-input will contain between 1 and 10 elements, inclusive.
-Each element of input will be between 0 and 2^b-1, inclusive.
-b will be between 1 and 31, inclusive.
-field_size will be between 1 and 31, inclusive.
-The size of input multipled by b will be greater than or equal to num_fields*field_size.
-Unused bits in the packet will be zeroes.
 

Examples

0)
    
{ 22, 37, 3 }
6
4
4
Returns: {19, 22, 6 }
This is the example from the problem statement.
1)
    
{ 1, 0 }
31
10
1
Returns: {512, 0 }

This 62-bit input packet has ten 1-bit fields, and 52 leading zeros. The first field (bit 0) is 1, and the other nine are 0.

In the output, the tenth field (bit 9) should be set to 1, and all other bits should be 0.

2)
    
{ 1, 0, 0, 0 }
10
31
1
Returns: {0, 0, 0, 1 }
3)
    
{ 15834, 2483, 19423 }
16
8
6
Returns: {53074, 60455, 27516 }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7642

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10735&pm=7642

Writer:

legakis

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Math