You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
463 B

7 years ago
  1. from functools import reduce
  2. from operator import mul
  3. def _base(n,b):
  4. out =[]
  5. while n:
  6. out.append(n%b)
  7. n //= b
  8. return out
  9. def _multbase(n,bases):
  10. out = []
  11. if functools.reduce(mul,bases) < n:
  12. raise ValueError("Not enough bases/not big enough")
  13. for base in reversed(bases):
  14. out.append(n%bases)
  15. n //= base
  16. if not n:
  17. return out
  18. def base(n,base_arg):
  19. try:
  20. return _multbase(n,base_arg
  21. except ValueError:
  22. return _base(n,base_arg)