Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. from heapq import *
  2. def main():
  3.     schedules = [
  4.        [(10, 29), (50, 99), (120, 200)],
  5.        [(25, 90), (130, 190)],
  6.        [(13, 20), (30, 90), (120, 180)],
  7.     ]
  8.    
  9.    
  10. main()
  11.  
  12. def findFree(schedules):
  13.     h = []
  14.     res = []
  15.     for i in range(len(schedules)):
  16.         heappush(h, (schedules[i][0][0], schedules[i][0][1], i, 0))
  17.        
  18.     while(h):
  19.         print(h)
  20.         (start, end, person, index) = heappop(h)
  21.         if(h):
  22.             (nextStart, nextEnd, nextPerson, nextIndex) = heappop(h)
  23.             if(end < nextStart):
  24.                 res.append([(end, nextStart)])
  25.             else:
  26.                 if(nextIndex != len(schedules[nextPerson]) - 1):
  27.                     for i in range(nextIndex, len(schedules[nextPerson])):
  28.                         if(schedules[nextPerson][i][0] > end):
  29.                             break
  30.                     heappush(h, (schedules[nextPerson][i][0], schedules[nextPerson][i][1], nextPerson, i))
  31.         else:
  32.             res.append([schedules[person][index - 1][1], start])
  33.         if(index + 1 < len(schedules[person])):
  34.                 heappush(h, (schedules[person][index + 1][0], schedules[person][index + 1][1], person, index + 1))
  35.            
  36.     return res
  37.            
  38. print findFree([
  39.        [(10, 29), (50, 99), (120, 200)],
  40.        [(25, 90), (130, 190)],
  41.        [(13, 20), (30, 90), (120, 180)],
  42.     ])
  43.            
  44.  
  45. def findOrder(strings):
  46.     nextDict = {}
  47.     countDict = {}
  48.     res = []
  49.    
  50.     # initialize dict
  51.     for string in strings:
  52.         for char in string:
  53.             nextDict[char] = []
  54.             countDict[char] = 0
  55.            
  56.     # build graph
  57.     for i in range(len(strings) - 1):
  58.         j = 0
  59.         fst = strings[i]
  60.         snd = strings[i + 1]
  61.         while(j < len(fst) and j < len(snd)):
  62.             if(fst[j] != snd[j]):
  63.                 nextDict[fst[j]].append(snd[j])
  64.                 countDict[snd[j]] += 1
  65.                 break
  66.             j += 1
  67.    
  68.     # generate topo sort
  69.     while(nextDict):
  70.         for (k, v) in nextDict.iteritems():
  71.             if(countDict[k] == 0):
  72.                 res.append(k)
  73.                 for node in v:
  74.                     countDict[node] -= 1
  75.  
  76.         for toDel in res:
  77.             if(toDel in nextDict):
  78.                 nextDict.pop(toDel, None)
  79.     return res
  80.            
  81. print(findOrder(['xa', 'xp', 'xc', 'pr']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement