Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
one-liner solution in Clear category for Compress List by Max0526
from collections.abc import Iterable
def compress(i: list[int]) -> Iterable[int]:
return [i[j] for j in range(len(i)) if i[j:j+1] != i[j+1:j+2]]
print("Example:")
print(list(compress([5, 5, 5, 4, 5, 6, 6, 5, 5, 7, 8, 0, 0])))
# These "asserts" are used for self-checking
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]
assert list(compress([9, 9, 9, 9, 9, 9, 9, 0, 9, 9, 9, 9, 9, 9])) == [9, 0, 9]
print("The mission is done! Click 'Check Solution' to earn rewards!")
April 27, 2023