Source: Ben Stephenson, The Python Workbook, Exercise 68: Parity Bits

Description

“Write a program that computes the parity bit for groups of 8 bits entered by the user using even parity.”

1
2
3
4
>>> solution('10010011')
'0'
>>> solution('10010010')
'1'

My solution

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
"""Parity bits."""

from cmd import Cmd


def solution(bp: str) -> str:
    """Return '1' or '0' to compute even parity of `bp`.

    `bp` represents an 8-bit pattern using '1' and '0'.

    >>> solution('10010011')
    '0'
    >>> solution('10010010')
    '1'
    """
    return "1" if bp.count("1") % 2 else "0"


class MyPrompt(Cmd):
    """Line-oriented command interpreter."""

    def do_solution(self, arg):
        """Handle input for `solution()`."""
        print("Input error" if len(arg) != 8 else solution(arg))


if __name__ == "__main__":
    MyPrompt().cmdloop()

Tests

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
37
38
39
40
41
42
43
44
45
46
47
48
"""Tests for solution."""

from hypothesis import given
from hypothesis.strategies import characters, text
from solution import solution


def test_solution_basic():
    """Hard-coded test data."""
    assert solution("10010011") == "0", "failed on even"
    assert solution("10010010") == "1", "failed on odd"


def test_solution_range():
    """Generated data."""

    def bp(i):
        s = "1" * i
        s += "0" * (8 - i)
        return s

    even = list(range(0, 9, 2))
    odd = list(range(1, 8, 2))

    for x in (bp(i) for i in even):
        assert solution(x) == "0", "failed on even"

    for x in (bp(i) for i in odd):
        assert solution(x) == "1", "failed on odd"


@given(
    s=text(
        alphabet=characters(min_codepoint=48, max_codepoint=49),
        min_size=8,
        max_size=8,
    )
)
def test_solution_prop(s):
    """Property test."""

    def count(str, chr):
        """Implement to avoid reproducing the solution."""
        return len([c for c in str if c == chr])

    assert (
        solution(s) == "1" if count(s, "1") % 2 else solution(s) == "0"
    ), "failed property test"