Define a new numerical base in python (new charset)

泄露秘密 提交于 2019-12-11 11:56:29

问题


I would like to know how to define a new numerical base in Python.

For example:

base dimension = 4
Charset = 'u', '$', '6', '}' (from the least important to the most)

I would like to know how to create and handle it, to be able to do simple arithmetic like:

$} + 6u * 6 = $$}
 7 +  8 * 2 =  23

I know I could use replace to replace u -> 0, $ -> 1 and so on, and use the int() function. However int() is not defined for base > 36, and I will have to handle these cases.

I know I could make my own function to convert them to base 10, do the math, and convert them back, but I would like to avoid that if possible.


回答1:


Rather than replace, you can use dictionaries to translate back and forth between the charset and regular ints, something like:

charset = 'u$6}'
b = len(charset) #base

vals = {c:i for i,c in enumerate(charset)}
digits = {vals[c]: c for c in vals} #inverse dictionary

def toInt(s):
    return sum(vals[c]*b**i for i,c in enumerate(reversed(s)))

def toNewBase(n):
    nums = [] if n > 0 else [0]
    while n > 0:
        n,r = divmod(n,b)
        nums.append(r)
    return ''.join(digits[i] for i in reversed(nums))

def add(s,t):
    return toNewBase(toInt(s) + toInt(t))

def subtract(s,t):
    return toNewBase(toInt(s) - toInt(t))

def multiply(s,t):
    return toNewBase(toInt(s) * toInt(t))

def divide(s,t):
    return toNewBase(toInt(s) // toInt(t))

typical output:

>>> add('$}',multiply('6u','6'))
'$$}'



回答2:


def str_base(number, base):
   # http://stackoverflow.com/a/24763277/3821804
   (d,m) = divmod(number,len(base))
   if d > 0:
      return str_base(d,base)+base[m]
   return base[m]



def charset(chars):
    class cls(int):
        __slots__ = ()

        def __new__(cls, src):
            if isinstance(src, str):
                return int.__new__(
                    cls,
                    ''.join(str(chars.index(i)) for i in src),
                    len(chars)
                )
            return int.__new__(cls, src)

        def __str__(self):
            return str_base(self, chars)

        def __repr__(self):
            return '%s(%r)' % (type(self).__name__, str(self))

    cls.__name__ = 'charset(%r)' % chars
    return cls

Usage:

test = charset('u$6}')
print(test( test('$}') + test('6u') * test('6') ) ) # => '$$}'

See it working online: http://rextester.com/WYSE48066

At the moment, I'm too tired to explain it.



来源:https://stackoverflow.com/questions/33240388/define-a-new-numerical-base-in-python-new-charset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!