Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. def findCommonPart(s1, s2):
  2.   common = (max(s1[0], s2[0]), min(s1[1], s2[1]))
  3.   if common[0] > common[1]:
  4.     return None
  5.   return common
  6.  
  7. def dist(s):
  8.   return abs(s[1] - s[0])
  9.  
  10. def solution(segments):
  11.   target = 0
  12.   otherTarget = None
  13.   common = segments[0]
  14.   for i, s in enumerate(segments[1:], 1):
  15.     newCommon = findCommonPart(common, s)
  16.     if not newCommon:
  17.       if not otherTarget:
  18.         otherTarget = i
  19.       else:
  20.         return target
  21.     else:
  22.       if dist(newCommon) < dist(common):
  23.         target = i
  24.  
  25.       common = newCommon
  26.  
  27.   return otherTarget
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement