Loop from 'aaaa' to 'tttt' ?

Anton Vredegoor anton at vredegoor.doge.nl
Wed Jun 18 11:28:14 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> wrote:

>> ... 7, 8, 9, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, etc.
>
>A very small modification to the normal base-representation
>algorithm does this:
>
>    def antoniandigits(val, base=10):
>        val = abs(val)
>        digits = []
>        while True:
>            q, r = divmod(val, base)
>            digits.append(r)
>            if not q:
>                break
>            val = q - 1                                 # no "- 1" normally
>        digits.reverse()
>        return digits
>
>Conversely,
>
>    def antonianvalue(string, base=10):
>        digits = list(string)
>        value = 0
>        for digit in digits:
>            value = value*base
>            value = value + ord(digits) - ord('0') + 1  # no "+ 1" normally
>        return value - 1                                # no "- 1" normally

Thanks a lot! I've already refactored it further into two reciprocal
functions: val <==> tuple:

def antoniandigits(val, base=10):
    tup = ()
    while val > -1:  
        tup = (val % base,) + tup
        val = val/base-1
    return tup

def antonianvalue(tup, base=10):
    return reduce(lambda x,y: x*base+y+1,tup,0)-1

>How about "Antonine" instead of "Antonian"?

Tough choice :-( Should I follow my heart and sail after Cleopatra,
resulting in an Antonine wall and having to use roman numerals for
ages, or stay with Octavia and have a loveless life, which however
would make it possible to have Antonians now and which would open up
possibilities for a great magic Celtic Empire?

Anton




More information about the Python-list mailing list