# assuming clock runs from 0 to 23 time1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] time2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] def timeOverlap(t1Start, t1End, t2Start, t2End): time1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] time2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] result = 0 # process time1 if t1End > t1Start: #t1 does not cross 0000 for n in range(t1Start, t1End+1): time1.pop(n) time1.insert(n,1) elif t1End < t1Start: #t1 cross 0000 for n in range(t1Start, 24): time1.pop(n) time1.insert(n,1) for n in range(0, t1End+1): time1.pop(n) time1.insert(n,1) # process time2 this portion can be refractored out if t2End > t2Start: #t2 does not cross 0000 for n in range(t2Start, t2End+1): time2.pop(n) time2.insert(n,1) elif t2End < t2Start: #t2 cross 0000 for n in range(t2Start, 24): time2.pop(n) time2.insert(n,1) for n in range(0, t2End+1): time2.pop(n) time2.insert(n,1) # compare time overlap for n in range(0,24): if time1[n] + time2[n] == 2: result = result + 1 return result print timeOverlap(7,10,9,11) # 1 print timeOverlap(8,10,9,12) # 1 print timeOverlap(2,20,1,21) # print timeOverlap(22,2,1,23) # 2
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question