# From: http://hypothesis.readthedocs.io/en/latest/quickstart.html#an-example
# 
# Install hypothesis like this
#     sudo apt install python3-pip
#     pip3 install hypothesis
# Run the tests
#     python3 hyp_ex1.py 

from hypothesis import given
from hypothesis.strategies import text

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

    
# Here are the two functions we want to test
def encode(input_string):
    # Q: assumptions on the input?
    if not input_string:
        return []
    count = 1
    prev = ''
    lst = []
    for character in input_string:
        if character != prev:
            if prev:
                entry = (prev, count)
                lst.append(entry)
            # Q: what happens if you forget to reset the counter below
            count = 1
            prev = character
        else:
            count += 1
    else:
        entry = (character, count)
        lst.append(entry)
    return lst


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

def showEnc(encLst):
    return ",".join( str(count) +  "x" + char for (char, count) in encLst )

# fixed test
if __name__ == '__main__':
    s="sss and ttttt"
    enc=encode(s)
    dec=decode(enc)
    print("Doing a run-length encoding of '" + s + "' gives \n'" + str(enc) + "'")
    print(showEnc(enc))
    print("Decoding it again gives '" + dec + "'")
    
# # kick-off the tests
if __name__ == '__main__':
   test_decode_inverts_encode()
