mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 08:51:31 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
21
bit_operations/convert_any_base.py
Normal file
21
bit_operations/convert_any_base.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
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