We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defjimOrders(orders):# Calculate total preparation time for each orderserve_time=[sum(order)fororderinorders]# Create and sort indexed list based on serve_timeindexed=sorted(enumerate(serve_time,1),key=lambdax:x[1])# Extract customer order indicescust_order=[index[0]forindexinindexed]returncust_order
Method 2 (Elaborated version):
defjimOrders(orders):# Initialize an empty list to store the total preparation time for each orderserve_time=[]# Loop through each order in the list of ordersfororderinorders:# Initialize the time for the current order to 0time=0# Loop through each preparation time in the current orderforprepinorder:# Add the preparation time to the current total timetime+=prep# Append the total time for the current order to the serve_time listserve_time.append(time)# Create a list of tuples where each tuple contains an index (starting from 1) and the corresponding serve timeindexed=list(enumerate(serve_time,1))# Sort the list of tuples based on the serve time (the second element of each tuple)indexed.sort(key=lambdax:x[1])# Create a list of indices representing the order in which customers should be served# Extract the first element of each tuple in the sorted list (which are the original indices)cust_order=[index[0]forindexinindexed]# Return the list of customer order indicesreturncust_order
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jim and the Orders
You are viewing a single comment's thread. Return to all comments →
Python3
Method 1 (simplified version):