Hackerrank - Jim and the Orders Solution
2 min read

Hackerrank - Jim and the Orders Solution

Hackerrank - Jim and the Orders Solution

Jim's Burgers has a line of hungry customers. Orders vary in the time it takes to prepare them. Determine the order the customers receive their orders. Start by numbering each of the customers from  to , front of the line to the back. You will then be given an order number and a preparation time for each customer.

The time of delivery is calculated as the sum of the order number and the preparation time. If two orders are delivered at the same time, assume they are delivered in ascending customer number order.

For example, there are  customers in line. They each receive an order number  and a preparation time .:Customer 1 2 3 4 5Order # 8 5 6 2 4Prep time 3 6 2 3 3Calculate:Serve time 11 11 8 5 7

We see that the orders are delivered to customers in the following order:Order by:Serve time 5 7 8 11 11Customer 4 5 3 1 2

Function Description

Complete the jimOrders function in the editor below. It should return an array of integers that represent the order that customers' orders are delivered.

jimOrders has the following parameter(s):

  • orders: a 2D integer array where each  is in the form .

Input Format

The first line contains an integer , the number of customers.
Each of the next  lines contains two space-separated integers, an order number and prep time for .


Output Format

Print a single line of  space-separated customer numbers (recall that customers are numbered from  to ) that describes the sequence in which the customers receive their burgers. If two or more customers receive their burgers at the same time, print their numbers in ascending order.

Sample Input 0

1 3
2 3
3 3

Sample Output 0

1 2 3

Explanation 0

Jim has the following orders:

  1. . This order is delivered at time .
  2. . This order is delivered at time .
  3. . This order is delivered at time .

The orders were delivered in the same order as the customers stood in line. The index in  is the customer number and is what is printed. In this case, the customer numbers match the order numbers.

Sample Input 1

8 1
4 2
5 6
3 1
4 3

Sample Output 1

4 2 5 1 3

Explanation 1

Jim has the following orders:

  1. . This order is delivered at time .
  2. . This order is delivered at time .
  3. . This order is delivered at time .
  4. . This order is delivered at time .
  5. . This order is delivered at time .

When we order these by ascending fulfillment time, we get:

  • : customer .
  • : customer .
  • : customer .
  • : customer .
  • : customer .

We print the ordered numbers in the bulleted listed above as 4 2 5 1 3.

Note: While not demonstrated in these sample cases, recall that any orders fulfilled at the same time must be listed by ascending order number.

Solution in Python

def jimOrders(orders):
    s = sorted(enumerate(orders,1),key=lambda x:sum(x[1]))
    return (i[0] for i in s)
n = int(input())
orders = [tuple(map(int,input().split())) for i in range(n)]

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.