Packets are sent to different ports on a computer system based on the hash of their packet ID. The value of the hash is as given below:
Hash = mod (packet_id,
number0fPorts) where mod is the modulus operator and takes the mod of first operand by second operand.
The ports are numbered from 0 to (number of ports) - 1, and a packet is initially sent to the port that has the port number equal to the hash of its packet ID. Each port requires t seconds to process an arriving packet. If a port is currently processing a packet, any arriving packet is rerouted to the next port number, and so on. The list of ports is circular. If a packet arrives at the last port and it is busy, it is rerouted to the first port. Given a list IDs of n packets that arrive 1 per second, find the port to which each packet is finally sent. The first packet is sent at second t=1.
Each port requires a time t to send a packet. If a port is currently sending a packet, this packet is then sent to the next port number, and so on. Given a list IDs of n packets that arrive 1 per second, find the port to which each packet is finally sent. The first packet is
sent at time t = 1.
Example
numberOfPorts = 3
transmission Time = 2
packet/ds = [4, 7, 10, 6]
The destination ports, assuming no time conflicts are all calculated as packetlds[i] modulo numberOfPorts, so [1, 1, 1, O]in this case. These arrive at times 1, 2, 3, 4.
The first packet is sent to port 1 with no conflicts. Port 1 will be occupied at times 1 and 2 due to the transmission time, so the second packet has a conflict and is
sent to port 1 + 1 = 2. The third packet
wants to go to port 1 and arrives at time 1,2,34. The first packet is sent to port 1 with no conflicts. Port 1 will be occupied at times 1 and 2 due to the transmission time, so the second packet has a conflict and is
sent to port 1 + 1 = 2. The third packet
wants to go to port 1 and arrives at time
3. Since port 1 is no longer transmitting packet 1, it receives the third packet. The fourth packet goes to port 0 without conflicts. The return array is [1, 2, 1, 0].
Function Description
Complete the sentTimesfunction in the editor below.
sentTimes has the following parameter(s):
int numberOfPorts. the number of ports in the system
int transmission Time: the time for a port to send a packet
int packet/ds[n]: the IDs of the packets in the order in which they arrive
Returns:
intIn]: the ports to which the packets are sent
Constraints
??1 ? numberOfPorts ? 2000
??1 ? transmission Time ? 100
??1?n? 2000
??1 ? packet/ds[i] ? 105
V Input Format For Custom Testing
The first line contains an integer, numberOfPorts, the number of ports in the system.
The next line contains an integer, transmission Time, the sending time for each port.
The next line contains an integer, n, the number of packets and size of packet/ds!].
Each line i of the n subsequent lines (where 0 ? i < n) contains an integer that describes packet/ds[i].