algorithm-in-python/math/numWeight/nega.py

27 lines
719 B
Python
Raw Normal View History

2019-06-11 16:26:24 +08:00
''' mbinary
#########################################################################
# File : nega.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2019-06-02 23:15
# Description:
#########################################################################
'''
2020-04-15 12:28:20 +08:00
def nega(n: int, base=-2: int)->: list:
2019-06-02 23:16:01 +08:00
'''return list of num, the first is the highest digit'''
2020-04-15 12:28:20 +08:00
if base > -2:
raise Exception(
f"[Error]: invalid base: {base}, base should be no more than -2")
2019-06-02 23:16:01 +08:00
ans = []
while n:
2020-04-15 12:28:20 +08:00
k = n % base
if k < 0:
k -= base
2019-06-02 23:16:01 +08:00
ans.append(k)
n = (n-k)//base
return ans[::-1]