binary.py

1
2
3
4
5
6
7
8
9
10
11
12
from functools import reduce


def parse_binary(digits):
    """Convert binary number (e.g. '101010' to decimal equivalent"""
    if not all(c in ("0", "1") for c in digits):
        raise ValueError

    return reduce(
        lambda acc, digit: acc * 2 + digit,
        map(int, digits)
    )

@cds-amal thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment