Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
groupby solution in Clear category for Compress List by vlad.bezden
"""Compress List.
https://py.checkio.org/en/mission/compress-list/
A given list should be "compressed" in a way so, instead of two
(or more) equal elements, staying one after another, there is
only one in the result Iterable (list, tuple, iterator ...).
Input: List.
Output: "Compressed" Iterable (list, tuple, iterator ...).
"""
from typing import List
from itertools import groupby
def compress(items: List[int]) -> List[int]:
return [key for key, _ in groupby(items)]
if __name__ == "__main__":
assert list(compress([5, 5, 5, 4, 5, 6, 6, 5, 5, 7, 8, 0, 0])) == [
5,
4,
5,
6,
5,
7,
8,
0,
]
assert list(compress([1, 1, 1, 1, 2, 2, 2, 1, 1, 1])) == [1, 2, 1]
assert list(compress([7, 7])) == [7]
assert list(compress([])) == []
assert list(compress([1, 2, 3, 4])) == [1, 2, 3, 4]
assert list(compress([9, 9, 9, 9, 9, 9, 9])) == [9]
print("PASSED!!!")
Aug. 8, 2020