1

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.

Dgn78
  • 21
  • 1
  • 1
    the work in converting to and from base 10 would be less than the work in directly implementing the arithmetical operations in your new char sets – John Coleman Oct 20 '15 at 15:28

2 Answers2

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'))
'$$}'
John Coleman
  • 51,337
  • 7
  • 54
  • 119
1
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.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52