Checkio笔记 - Create intervals(generator version)

From a set of ints you have to create a list of closed intervals as tuples, so the intervals are covering all the values found in the set.
In this mission you should use the 'yield' to make your function a generator.

A closed interval includes its endpoints! The interval 1..5, for example, includes each value x that satifies the condition 1 <= x <= 5.

Values can only be in the same interval if the difference between a value and the next smaller value in the set equals one, otherwise a new interval begins. Of course, the start value of an interval is excluded from this rule.
A single value, that does not fit into an existing interval becomes the start- and endpoint of a new interval.

Input:A set of ints.

Output:A list of tuples of two ints, indicating the endpoints of the interval. The list should be sorted by start point of each interval.
Examples:
create_intervals({1, 2, 3, 4, 5, 7, 8, 12}) == [(1, 5), (7, 8), (12, 12)] create_intervals({1, 2, 3, 6, 7, 8, 4, 5}) == [(1, 8)]

我的代码:

def create_intervals(data):
    a = []
    b = []
    for i in data:
        if i-1 not in data:
            a.append(i)
        if i+1 not in data:
            b.append(i)
    a.sort()
    b.sort()
    for j in zip(a,b):
        yield j
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文网址:https://kbroman.org/qtl2/assets/vignettes/user_guide...
    耕读者阅读 1,304评论 0 1
  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 4,357评论 0 13
  • 感觉自己有点神经衰弱,总是觉得手机响了;屋外有人走过;每次妈妈不声不响的进房间突然跟我说话,我都会被吓得半死!一整...
    章鱼的拥抱阅读 2,226评论 4 5
  • 夜莺2517阅读 127,764评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 6,976评论 1 6