forked from coinse/cs453-demo-pbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_decode.py
36 lines (30 loc) · 875 Bytes
/
encode_decode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from hypothesis import given
from hypothesis.strategies import text
# example taken from Hypothesis quickstart
# (https://hypothesis.readthedocs.io/en/latest/quickstart.html)
# is given.
# below, a lossless encoding-decoding algorithm is provided.
def encode(input_string):
count = 1
prev = ""
lst = []
for character in input_string:
if character != prev:
if prev:
entry = (prev, count)
lst.append(entry)
count = 1
prev = character
else:
count += 1
entry = (character, count)
lst.append(entry)
return lst
def decode(lst):
q = ""
for character, count in lst:
q += character * count
return q
# We want to know that the decode function is indeed the
# inverse of the encode function. How would we express this in
# hypothesis?