def simplify_repeats(list_pattern):
    """
    Converts ['a','a','b','a','b', 'b','b'] to ['a!2', 'b','a','b!3']
    """
    n_repeats = 0
    output_list = []
    for i, x in enumerate(list_pattern):
        # if not the last element
        if i != len(list_pattern) - 1:
            # if the next element is a repeat
            # increment the counter
            if x == list_pattern[i + 1]:
                n_repeats += 1
            # if next element is different and current element is not a repeat.
            elif n_repeats == 0:
                output_list.append(x)
            # otherwise there was a repeat that terminates now.
            else: 
                new_x = str(x) + '!' + str(n_repeats + 1)
                output_list.append(new_x) 
                n_repeats = 0
        # handle last element
        else:
            # simple case, last element is not a repeat
            if n_repeats == 0:
                output_list.append(x)
            # the penultimate position matches the last. 
            else:
                new_x = str(x) + '!' + str(n_repeats + 1)
                output_list.append(new_x) 
                n_repeats = 0

    return output_list