Hackbright Code Challenges

Truncate Repeats: Solution

Truncate Repeats: Solution

Problem

Truncate Repeats

Whiteboard

Easier

Challenge

Easier

Concepts

Strings, Loops

Download

truncate-repeats-solution.zip


def truncate(string):
    """Truncate repeating characters in a string."""

    # START SOLUTION

    result = []
    for ch in string:
        if not result or ch != result[-1]:
            result.append(ch)

    return ''.join(result)