Recursion and Variable Scope
Doug Landauer
landauer at apple.com
Fri Sep 7 13:56:30 EDT 2001
More information about the Python-list mailing list
Fri Sep 7 13:56:30 EDT 2001
- Previous message (by thread): Recursion and Variable Scope
- Next message (by thread): Recursion and Variable Scope
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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. def ib ( i, b ): if i<b: return `i` return ib(i/b,b) + `i%b` def ibx ( i, b, d ): ibr = ib(i,b) while len(ibr) < d: ibr= '0'+ibr return ''.join( [ chr( ord(ch)+1 ) for ch in ibr ] ) def dicide1 ( sides, dice ): """ silly version, too bad it needs the "ib" aux function. """ lows= '0'*dice return [ [ int(ch) for ch in ibx(nr,sides,dice) ] \ for nr in range(int(lows,sides), int('1'+lows,sides) ) ] def dicide2 ( sides, dice ): """ way silly version, slower. """ lows= '0'*dice return filter( lambda lst, d=dice: len(lst) == d, [ [ int(ch) for ch in `nr` if ch in '123456789'[:sides] ] \ for nr in range(int(lows), int('1'+lows) ) ] ) def seeThem () : print '\nThis just counts in base "dice" ...' print 'dicide1:' print dicide1( 4, 2 ) print '\nThis counts in base ten, and throws many away ...' print 'dicide2:' print dicide2( 4, 2 ) -- Doug Landauer landauer at got dot net
- Previous message (by thread): Recursion and Variable Scope
- Next message (by thread): Recursion and Variable Scope
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list