2017-08-03 24 views
-1

数字と時間の重なりを検出するためにこれを書いたので、今度は同じ曜日にチェックする機能を追加したいが、この条件を何も追加しようとしなかった印刷されます。時間と日付が重複する

intervals = [[100,200, "M", "math"],[100,200, "T", "calc"], [150,250, "M", "eng"],[300,400, "W", "design"], [50,250, "T", "bio"]] 
# s = start e = end d = day 
overlapping = [ [s,e,d] for s in intervals for e in intervals for d in intervals if s is not e and s[1]>e[0] and s[0]<e[0] and d[1] == d[0] or s[0]==e[0] and s[1]==e[1] and d[1] == d[0] and s is not e] 

for x in overlapping: 
    print '{0} overlaps with {1}'.format(x[0],x[1]) 


''' 
expected: 

[100,200, "M", "math"] overlaps with [150,250, "M", "eng"] 
[100,200, "T", "calc"] overlaps with [50,250, "T", "bio"] 

''' 

私のロジックに何が間違っていますか?

答えて

1

2つのクラス間の時間の重複をチェックするには、2つのループで十分だと思います。

intervals = [[100,200, "M", "math"],[100,200, "T", "calc"], [150,250, "M", "eng"],[300,400, "W", "design"], [50,250, "T", "bio"]] 

# not same object 
# same weekday 
# time overlap 
overlapping = [[s,e] for s in intervals for e in intervals 
      if s is not e 
      and s[2]==e[2] 
      and (s[0]<e[1] and s[1]>e[0])] 

for x in overlapping: 
    print('{0} overlaps with {1}'.format(x[0],x[1])) 

は(あなたは、重複を削除したい場合は、一歩が必要になります)、

[100, 200, 'M', 'math'] overlaps with [150, 250, 'M', 'eng'] 
[100, 200, 'T', 'calc'] overlaps with [50, 250, 'T', 'bio'] 
[150, 250, 'M', 'eng'] overlaps with [100, 200, 'M', 'math'] 
[50, 250, 'T', 'bio'] overlaps with [100, 200, 'T', 'calc'] 
0

を取得する複数のクラスがある場合、私はを占めることができるようにWonjinの答えにもう少し追加回、このタイプの繰り返しを探している人のために、私のコードは

intervals = [ [[100,200, "M", "math"],[100,200, "T", "math"]], [[100,200, "M", "eng"]], [[300,400, "W", "design"]], [[50,250, "T", "bio"]] ] 

# not same object 
# same weekday 
# time overlap 
overlapping = [ [s,e] for s in intervals for x in s for e in intervals for y in e if s is not e and x[2]==y[2] and (x[0]<y[1] and x[1]>y[0]) ] 

for x in overlapping: 
    print('{0} overlaps with {1}'.format(x[0],x[1])) 


Output: 
[[100, 200, 'M', 'math'], [100, 200, 'T', 'math']] overlaps with [[100, 200, 'M', 'eng']] 
[[100, 200, 'M', 'math'], [100, 200, 'T', 'math']] overlaps with [[50, 250, 'T', 'bio']] 
[[100, 200, 'M', 'eng']] overlaps with [[100, 200, 'M', 'math'], [100, 200, 'T', 'math']] 
[[50, 250, 'T', 'bio']] overlaps with [[100, 200, 'M', 'math'], [100, 200, 'T', 'math']] 
関連する問題