fork(1) download
  1. # (c) http://stackoverflow.com/a/16315169
  2. # build ranges first
  3. def expand(list):
  4. newList = []
  5. for r in list:
  6. newList.append(range(r[0], r[1] + 1))
  7. return newList
  8.  
  9.  
  10. def compare(list):
  11. toBeDeleted = []
  12. for index1 in range(len(list)):
  13. for index2 in range(len(list)):
  14. if index1 == index2:
  15. # we dont want to compare ourselfs
  16. continue
  17. matches = [x for x in list[index1] if x in list[index2]]
  18. if len(matches) != 0: # do we have overlap?
  19. ## compare lengths and get rid of the longer one
  20. if len(list[index1]) > len(list[index2]):
  21. toBeDeleted.append(index1)
  22. break
  23. elif len(list[index1]) < len(list[index2]):
  24. toBeDeleted.append(index2)
  25. # distinct
  26. toBeDeleted = [ toBeDeleted[i] for i,x in enumerate(toBeDeleted) if x not in toBeDeleted[i+1:]]
  27. print len(list)
  28. # remove items
  29. for i in toBeDeleted[::-1]:
  30. del list[i]
  31. return list
  32.  
  33. import ast, sys
  34. a = ast.literal_eval(sys.stdin.read())
  35. print("Input: %s" % (a,))
  36. print(compare(expand(a)))
Success #stdin #stdout 0.08s 8840KB
stdin
[[0, 10], [9, 12], [11, 20]]
stdout
Input: [[0, 10], [9, 12], [11, 20]]
3
[[9, 10, 11, 12]]