Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import re, math
  3. from functools import reduce
  4.  
  5. hex_table = ['a', 'b', 'c', 'd', 'f']
  6.  
  7. def bin_to_dec(bin):
  8. assert(re.match("^[01]+$", bin))
  9. return reduce(lambda x, y: x + y, [(2**i) for i, x in enumerate(bin[::-1]) if x == '1'])
  10.  
  11. def hex_to_dec(hex):
  12. assert(re.match("^[0-9a-f]+$", hex, re.I))
  13. return reduce(lambda x, y: x + y, [int(hex_table.index(x) + 11 if x in hex_table else x) * (16 ** i) for i, x in enumerate(hex[::-1])])
  14.  
  15. def dec_to_hex(n):
  16. assert(isinstance(n, int) or n.isnumeric())
  17. return None
  18.  
  19. def dec_to_bin(n):
  20. assert(isinstance(n, int) or n.isnumeric())
  21. return None
  22.  
  23. def factoral(n):
  24. return reduce(lambda x, y: x * y, [z for z in range(n, 0, -1)])
  25.  
  26. def quadratic(a, b, c):
  27. return ((-b + math.sqrt(b ** 2 - 4 * a * c)) / 2 * a,
  28. (-b - math.sqrt(b ** 2 - 4 * a * c)) / 2 * a)
  29.  
  30.  
  31. print(factoral(5))
  32. print(bin_to_dec("01101110"))
  33. print(dec_to_bin(110))
  34. print(hex_to_dec("3f48"))
  35. print(dec_to_hex(16200))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement