mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 01:41:20 -04:00
play with bits
This commit is contained in:
parent
c77eb0dd00
commit
4b22537ee7
5 changed files with 73 additions and 0 deletions
16
bit_manipulation/convert_any_base.py
Normal file
16
bit_manipulation/convert_any_base.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
def convert_to_any_base(base: int, num: int) -> str:
|
||||
|
||||
if num == 0:
|
||||
return "0"
|
||||
|
||||
n = abs(num)
|
||||
result = ""
|
||||
|
||||
while n:
|
||||
result += str(n % base)
|
||||
n //= base
|
||||
|
||||
if num < 0:
|
||||
result += '-'
|
||||
|
||||
return result[::-1]
|
Loading…
Add table
Add a link
Reference in a new issue