You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
819 B

def group_equal(els):
result = []
current = []
for element in els:
if len(current) == 0 or element in current:
current.append(element)
else:
result.append(current)
current = [element]
if len(current) != 0:
result.append(current)
return result
if __name__ == '__main__':
print("Example:")
print(group_equal([1, 1, 4, 4, 4, "hello", "hello", 4]))
# These "asserts" are used for self-checking and not for an auto-testing
assert group_equal([1, 1, 4, 4, 4, "hello", "hello", 4]) == [[1, 1], [4, 4, 4], ["hello", "hello"], [4]]
assert group_equal([1, 2, 3, 4]) == [[1], [2], [3], [4]]
assert group_equal([1]) == [[1]]
assert group_equal([]) == []
print("Coding complete? Click 'Check' to earn cool rewards!")