Facebook
From Emerald Pelican, 5 Years ago, written in Python.
This paste is a reply to Untitled from Perl Peccary - view diff
Embed
Download Paste or View Raw
Hits: 237
  1. import heapq
  2. def getAvgWaitTime(n, cust):
  3.     heap = []
  4.     curTime = 0
  5.     waitTime = 0
  6.     cust = sorted(cust)
  7.     i = 0
  8.  
  9.     while i < n:
  10.         if curTime < cust[i][0]:
  11.             curTime = cust[i][0]
  12.         while i < n and (curTime >= cust[i][0]):
  13.             heapq.heappush(heap, (cust[i][1], cust[i][0]))
  14.             i += 1
  15.  
  16.         while (i < n) and curTime < cust[i][0] and len(heap) > 0:
  17.             time, wait = calculateWaiting(heap, curTime)
  18.             curTime += time
  19.             waitTime += wait
  20.  
  21.     # Clear all the jobs
  22.     while len(heap) > 0:
  23.         time, wait = calculateWaiting(heap, curTime)
  24.         curTime += time
  25.         waitTime += wait
  26.  
  27.     return waitTime / n
  28.  
  29.  
  30. def calculateWaiting(heap, curTime):
  31.     wait = 0
  32.     cur = heapq.heappop(heap)
  33.     wait = curTime - cur[1] + cur[0]
  34.     return (cur[0], wait)
  35.  
  36.  
  37. n = int(input().strip())
  38. cust = []
  39. for i in range(n):
  40.     ar = list(map(int, input().strip().split(' ')))
  41.     cust.append((ar[0], ar[1]))
  42.  
  43.  
  44. result = getAvgWaitTime(n, cust)
  45. print(result)

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Unique Prairie Dog python 5 Years ago.