Skip to content

Instantly share code, notes, and snippets.

@matthiasgoergens
Created June 24, 2017 23:51
Show Gist options
  • Save matthiasgoergens/c37c5408fac9ff6769d82043cb38c57b to your computer and use it in GitHub Desktop.
Save matthiasgoergens/c37c5408fac9ff6769d82043cb38c57b to your computer and use it in GitHub Desktop.
Runlegth encoding tested via hypothesis
#!/usr/local/bin/python3.6
# -*- coding: utf-8 -*-
from hypothesis import given, example
import hypothesis.strategies as st
def decode(l):
s = ""
for i, c in l:
s += i * c
return s
@given(s=st.text())
def trivial(s):
assert decode([(1, c) for c in s]) == s
def encode(s):
"""Run length encoding of s as a list of tuples [(repeats, character)]"""
raise NotImplementedError
@given(s=st.text())
def round_trip(s):
enc = encode(s)
dec = decode(enc)
assert s == dec, "{} != {} == decode({}) == decode(encode({}))".format(s, dec, enc, s)
@given(s=st.text())
def no_adjacent(s):
last = None
enc = encode(s)
for i, c in enc:
assert last != c, "{} == {}".format(last, c)
last = c
if __name__ == '__main__':
trivial()
round_trip()
no_adjacent()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment