2018-04-02

def _stub_rectangle_list(rectangle_list, query_rectangle):
    """Pop rectangles overlapped by query_rectangle from rectangle_list."""
    new_rectangle_list = []
    stubbed_rectangle_list = []
    while rectangle_list:
        rectangle = rectangle_list.pop()
        if rectangle & query_rectangle:
            stubbed_rectangle_list.append(rectangle)
        else:
            new_rectangle_list.append(rectangle)
    rectangle_list.extend(new_rectangle_list)
    return stubbed_rectangle_list


def _stub_rectangle_list(rectangle_list, query_rectangle):
    ###another implementation using list comprehension.
    ###It's slower than the above code, but more readable.
    new_rectangle_list
        = [rec for rec in rectangle_list if not (rec & query_rectangle)]
    stubbded_rectangle_list
        = [rec for rec in rectangle_list if rec & query_rectangle]

    # rectangle_list ← new_rectangle_list
    rectangle_list.clear()
    rectangle_list.extend(new_rectangle_list)

    return stubbded_rectangle_list

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん