Advertisement
VSZM

Untitled

Nov 26th, 2019
253
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/python3
  2. # Big Fat Indian Wedding
  3. import math
  4. import os
  5. import random
  6. import re
  7. import sys
  8.  
  9.  
  10.  
  11. #
  12. # Complete the 'maxPerformances' function below.
  13. #
  14. # The function is expected to return an INTEGER.
  15. # The function accepts following parameters:
  16. #  1. INTEGER_ARRAY arrivals
  17. #  2. INTEGER_ARRAY durations
  18. #
  19.  
  20. def isThereAConflict(arrivals_sub, durations_sub):
  21.     start = arrivals_sub[0]
  22.     end = arrivals_sub[0] + durations_sub[0]
  23.  
  24.     for i in range(1, len(arrivals_sub)):
  25.         start = arrivals_sub[i]
  26.         if start < end:
  27.             return True
  28.         end = start + durations_sub[i]
  29.  
  30.     return False
  31.  
  32.  
  33. def maxPerformances(arrivals, durations):
  34.     #sorting
  35.     zipped = list(zip(arrivals, durations))
  36.     sorted_zipped = sorted(zipped)
  37.     print(sorted_zipped)
  38.     arrivals = [item[0] for item in sorted_zipped]
  39.     durations = [item[1] for item in sorted_zipped]
  40.  
  41.  
  42.     # Algo expects sorted by arrival time
  43.     max_perf = 0
  44.     for i in range(len(arrivals)):
  45.         selected_arrivals = [arrivals[i]]
  46.         selected_durations = [durations[i]]
  47.  
  48.         for j in range(i, len(arrivals)):
  49.             if not isThereAConflict(selected_arrivals + [arrivals[j]], selected_durations + [durations[j]]):
  50.                 selected_arrivals.append(arrivals[j])
  51.                 selected_durations.append(durations[j])
  52.             if len(selected_arrivals) > max_perf:
  53.                 max_perf = len(selected_arrivals)
  54.                 print(f'New longest sublist! Selected arrivals: {selected_arrivals}, Selected durations: {selected_durations}')
  55.  
  56.     return max_perf
  57.  
  58. if __name__ == '__main__':
  59.     print(maxPerformances([1, 3, 3, 5, 7], [2, 2, 1, 2, 1]))
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement