我收到错误信息
Traceback:
in <module>
in merge_ranges
TypeError: 'int' object is not iterable
有人可以建议出什么问题吗,也可以用于调试吗?
meets = [(1,3),(5,8),(8,14),(13,17)]
print '{} should be {}'.format(merge_ranges(meets))
def merge_ranges(meets):
#sort by start time
sorted_meetings = sorted(meets)
merged_meetings = sorted_meetings[0]
for current_meeting_start, current_meeting_end in sorted_meetings[1:]:
last_merged_meeting_start, last_merged_meeting_end = merged_meetings[-1]
if (current_meeting_start <= last_merged_meeting_end):
merged_meetings[-1] = (last_merged_meeting_start, max(last_merged_meeting_end, current_meeting_end))
else:
merged_meetings.append((current_meeting_start, current_meeting_end))
# write the body of your function here
return merged_meetings
最佳答案
您将变量merged_meetings
初始化为单个元组,但是它应该是元组列表。
因此,您必须将merged_meetings
的初始化修改为
merged_meetings = [sorted_meetings[0]]
测试用例的结果将是
[(1, 3), (5, 17)]