Numeric literals in other than base 10 - was Annoying octal notation
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Tue Aug 25 10:14:49 EDT 2009
On Mon, 24 Aug 2009 18:01:38 -0700, Mensanator wrote:
>> If you want your data file to have values entered in hex, or oct, or
>> even unary (1=one, 11=two, 111=three, 1111=four...) you can.
>
> Unary? I think you'll find that Standard Positional Number Systems are
> not defined for radix 1.
Of course not. But unary isn't a positional number system. It's a tally
system, like my example above shows. Roman numerals are another tally
system. Like Roman numerals, the disadvantages of unary are that you
can't represent negative numbers, zero, or fractions, and anything but
addition and subtraction is difficult. But if you want to use it, perhaps
out of a sense of sadism towards your users, it's easy:
def int2unary(n):
return '1'*n
def unary2int(s):
n = 0
for c in s:
if c == '1': n+=1
else: raise ValueError('invalid unary string')
return n
--
Steven
More information about the Python-list
mailing list