Recursion and Variable Scope

Doug Landauer landauer at got.dot.net.invalid
Fri Sep 7 15:19:24 EDT 2001


In article <landauer-8F040B.10563007092001 at news.apple.com>, Doug 
Landauer <landauer at apple.com> wrote:

> In article <9n9vln072d at enews1.newsguy.com>, "Alex Martelli" 
> <aleax at aleax.it> wrote:
>   [... A very good exposition ...]
> > 
> > Personally, I find it clearest to frame such enumeration
> > problems as "counting in a certain base"...
> 
> Here are the two silly versions that I came up with last night,
> using that theme ... they probably won't work for sides > 9.

Here's a cleaner (well, *I* like it better) version of dicide1:

def bx ( i,b,d,l ):
    if d: return bx( i/b, b, d-1, [i%b+1] + l )
    else: return l

def dicide4 ( sides, dice ):
    lows= '0'*dice
    return [ bx(nr,sides,dice,[]) \
               for nr in range(int(lows,sides), int('1'+lows,sides) ) ]

print dicide4(4,2)
-- 

    Doug Landauer     landauer at got dot net



More information about the Python-list mailing list