Skip to content

Instantly share code, notes, and snippets.

@brunomsantiago
Last active February 9, 2023 02:13
Show Gist options
  • Save brunomsantiago/1983ab1150c17f058330fb17e0f305ba to your computer and use it in GitHub Desktop.
Save brunomsantiago/1983ab1150c17f058330fb17e0f305ba to your computer and use it in GitHub Desktop.
Check if all items of a list of string are grouped. ['a', 'a', 'b'] are, ['a', 'b, 'a'] are not.
def are_strings_grouped(list_of_strings):
# First find the number of expected changes
n_unique_strings = len(set(list_of_strings))
n_expected_changes = n_unique_strings - 1
# Then find the real number of changes
groups_channges = [1 for former, latter
in zip(list_of_strings[:-1], list_of_strings[1:])
if former != latter]
n_real_changes = sum(groups_channges)
# Finally compare them
return n_real_changes == n_expected_changes
# Grouped:
# _list_of_strings = ['a', 'a', 'a', 'b', 'b', 'b', 'c']
# _list_of_strings = ['a', 'a', b']
# _list_of_strings = ['a', 'b', b']
# _list_of_strings = ['a', 'b', c']
# _list_of_strings = ['a', 'b']
# _list_of_strings = ['a', 'a']
# _list_of_strings = ['a']
#
# Not Grouped:
# _list_of_strings = ['a', 'a', 'a', 'b', 'b', 'b', 'a']
# _list_of_strings = ['a', 'b', 'a', 'b', 'b', 'b', 'c']
# _list_of_strings = ['a', 'b', 'a', 'c', 'b', 'c', 'd']
# _list_of_strings = ['a', 'b', 'a', 'b', 'a', 'b', 'a']
# _list_of_strings = ['a', 'b', a']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment