hexadecimal.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from functools import reduce
import string


HEX_VALUES = dict(zip(
    string.hexdigits,
    range(0, 16)
))


def hexa(digits):
    """Convert hexadecimal string to decimal equivalent"""
    digits = digits.lower()

    if not all(c in HEX_VALUES.keys() for c in digits):
        raise ValueError

    return reduce(
        lambda acc, digit: acc * 16 + digit,
        (HEX_VALUES[c] for c in digits)
    )

Comments


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