from hypothesis import given
from hypothesis.strategies import text

# Run-length encoder
# aaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccccc ddddddddddddddd eeeeeeeeeeee
# 19a 18b …
def encode(input_string):
    count = 1
    prev = ''
    lst = []

    if not input_string:
        return ([], 0)

    for character in input_string:
        if character != prev:
            if prev:
                entry = (prev,count)
                lst.append(entry)
            count = 1
            prev = character
        else:
            count += 1
    else:
        try:
            entry = (character,count)
            lst.append(entry)
            return (lst, 0)
        except Exception as e:
            print("Exception encountered {e}".format(e=e))
            return (e, 1)

def decode(lst):
    q = ""
    for character, count in lst:
        q += character * count
    return q

@given(text())
def test_decode_inverts_encode(s):
    lst, err = encode(s)
    assert decode(lst) == s
