美文网首页
Checkio笔记 - Create intervals(gen

Checkio笔记 - Create intervals(gen

作者: 鹿呀鹿呀快开门 | 来源:发表于2020-06-02 14:04 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:Checkio笔记 - Create intervals(gen

          本文链接:https://www.haomeiwen.com/subject/ulsuzhtx.html