Define two functions: `hex_to_bin` and `bin_to_hex` (or `hexToBin` and `binToHex`)


# hex_to_bin

Takes a hexadecimal string as an argument .

**Note:** This string can contain upper or lower case characters and start with any amount of zeros.

Returns the binary representation (without leading zeros) of the numerical value of the hexadecimal string.

Examples:

```
"00F"    -->  "1111"
"5"      -->  "101"
"00000"  -->  "0"
"04D2"   -->  "10011010010"
```

# bin_to_hex

Takes a binary string (with or without leading zeros) as an argument .

Returns the hexadecimal representation of the numerical value of the binary string.

**Note:** Any non-numerical characters should be lower case


Examples:

```
"1111"         -->  "f"
"000101"       -->  "5"
"10011010010"  -->  "4d2"
```

**Note:** You can assume all arguments are valid so there is no need for error checking.

Oh, and I've disabled a few things.


Any feedback would be much appreciated

Difficulty: introductory

Starter code:

def bin_to_hex(binary_string):
	

Solutions:
bin2hex = {"0000": "0", "0001": "1", "0010": "2", "0011": "3", 
           "0100": "4", "0101": "5", "0110": "6", "0111": "7", 
           "1000": "8", "1001": "9", "1010": "a", "1011": "b", 
           "1100": "c", "1101": "d", "1110": "e", "1111": "f"}
hex2bin = {v: k for k, v in bin2hex.items()}

def bin_to_hex(s, res = ""):
    s = "0" * (4 - len(s) % 4) + s
    while s:
        res += bin2hex[s[:4]]
        s = s[4:]
    return res.lstrip("0") or "0"
    
def hex_to_bin(s, res = ""):
    while s:
        res += hex2bin[s[0].lower()]
        s = s[1:]
    return res.lstrip("0") or "0"

D_S = '0123456789abcdef'
S_D = {d:n for n,d in enumerate(D_S)}

def convert(s,bIn,bOut):
    n = sum( S_D[d] * bIn**p for p,d in enumerate(reversed(s.lower())))
    out=[]
    while n:
        n,r = divmod(n,bOut)
        out.append(D_S[r])
    return ''.join(reversed(out)) or '0'
    
def bin_to_hex(s): return convert(s,2,16)
def hex_to_bin(s): return convert(s,16,2)

def bin_to_hex(bin_stg):
    return base_to_base(bin_stg, 2, 16)

def hex_to_bin(hex_stg):
    return base_to_base(hex_stg, 16, 2)

def base_to_base(num_stg, from_base, to_base):
    digits = "0123456789abcdef"
    result, n = "", sum(digits.index(d) * from_base**i for i, d in enumerate(num_stg.lower()[::-1]))
    while n > 0:
        d = n % to_base
        result, n = digits[d] + result, n // to_base
    return result or "0"

h = '0123456789abcdef'

def bin_to_hex(binary_string):
    n = 0
    for x in binary_string:
        n = n * 2 + (x == '1')
    result = []
    while n:
        n, r = divmod(n, 16)
        result.append(h[r])
    return ''.join(result[::-1]) or '0'

def hex_to_bin(hex_string):
    n = 0
    for x in hex_string.lower():
        n = n * 16 + h.find(x)
    result = []
    while n:
        n, r = divmod(n, 2)
        result.append(h[r])
    return ''.join(result[::-1]) or '0'

def bin_to_hex(binary_string):

  n = sum( 2**i for i, b in enumerate(binary_string[::-1]) if b == '1' )
  
  return '{:x}'.format(n)
    
def hex_to_bin(hex_string):

  convert = lambda x: ord(x) - 48 if 48 <= ord(x) <=57 else ord(x.lower()) - 87
  
  n = sum( convert(x) * 16**i for i, x in enumerate(hex_string[::-1]) )

  return '{:b}'.format(n)

def from_base(string, base, alphabet='0123456789abcdef'):
    return sum(alphabet.index(c) * base ** i for i, c in enumerate(string.lower()[::-1]))

def to_base(n, base, alphabet='0123456789abcdef'):
    s = ''
    while n:
        n, m = divmod(n, base)
        s = alphabet[m] + s
    return s or alphabet[0]

def bin_to_hex(string):
    return to_base(from_base(string, 2), 16)
    
def hex_to_bin(string):
    return to_base(from_base(string, 16), 2)

N = '0123456789abcdef'

def converter(s, p1, p2):
    """ Convert input string to desired output """
    dec = sum(N.find(s[~i].lower()) * (p1 ** i) for i in range(len(s)))
    res = []
    while dec != 0:
        res.append(dec % p2)
        dec //= p2
    return ''.join(N[i] for i in reversed(res)) if res else '0'

def bin_to_hex(b):
    return converter(b, 2, 16)
    
def hex_to_bin(h):
    return converter(h, 16 ,2)

bh={"":"","0000":"0","0001":"1","0010":"2","0011":"3","0100":"4","0101":"5","0110":"6","0111":"7","1000":"8","1001":"9","1010":"a","1011":"b","1100":"c","1101":"d","1110":"e","1111":"f"}
def bin_to_hex(bs):
    r=""; bs="000"+bs
    for i in range(len(bs),-1,-4): s=bs[i-4:i]; r=bh[s]+r
    while r[0]=="0" and len(r)>1: r=r[1:]   
    return r

hb={bh[v]:v for v in bh}
def hex_to_bin(hs):
   r=""; hs=hs.lower()
   for c in hs: r+=hb[c]
   while r[0]=="0" and len(r)>1: r=r[1:]
   return r

val  = lambda c: ord(c.lower()) - (48 if ord(c) < 58 else 87)
int_ = lambda s, x: sum(val(c) * x**i for i,c in enumerate(reversed(s)))

def bin_to_hex(binary_string):
    return "{:x}".format(int_(binary_string, 2))
    
def hex_to_bin(hex_string):
    return "{:b}".format(int_(hex_string, 16))

int = 0 .__class__
str = 'a'.__class__

CONVERSION = dict(enumerate("0123456789abcdef"))


def bin_to_hex(b):
    b = int(b, 2)
    result = []
    while True:
        b, m = divmod(b, 16)
        result.append(CONVERSION[m])
        if b == 0:
            break
    return "".join(map(str, reversed(result)))


def hex_to_bin(b):
    b = int(b, 16)
    result = []
    while True:
        b, m = divmod(b, 2)
        result.append(m)
        if b == 0:
            break
    return "".join(map(str, reversed(result)))