```
def val(number_sequence, base):
""" produce integer value of number_sequence as base number """
return sum(int(n) * base ** p for p, n in enumerate(reversed(number_sequence)))
def balanced_ternary_value(number):
""" produce integer value of balanced ternary number """
return val([(-1 if c == '-' else (1 if c == '+' else 0))
for c in str(number)], 3)
def make_balanced(n):
""" Make balanced ternary by doing carry at
2 and using identity 2 = 3 + -1
"""
n = int(n)
if not n:
return '0'
else:
digits = ''
while n:
n, this = divmod(n, 3)
if this == 2:
n += 1
digits = '0+-'[this] + digits
return digits
class BalancedTernary(long):
def __new__(cls, n):
instance = long.__new__(cls,
balanced_ternary_value(n)
if isinstance(n, str) else n)
return instance
def __repr__(self):
return make_balanced(self)
__str__ = __repr__
if __name__ == '__main__':
print('%10s %10s' % ('dec', 'balanced ternary'))
for t in range(-27,+28):
print('%10s %10s' % (t, BalancedTernary(t)))
assert balanced_ternary_value(str(BalancedTernary(t))) == t
print balanced_ternary_value('++-0+')
print BalancedTernary('++-0+'), int(BalancedTernary('++-0+'))
```

About the Author

1

Contributor0

Replies3

Views**Are you able to help answer this sponsored question?**

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Recommended Topics