all possible combinations

Steve Holden steve at holdenweb.com
Thu Jul 28 13:30:23 CEST 2005


Anton Vredegoor wrote:
> John Machin wrote:
> 
> 
>>You don't need to use random sampling. Paul Rubin has shown how it can
>>be done deterministically. The following is a generalisation of his
>>code; it generates all possible assemblies of size n from a list of
>>parts. Is this helpful?
>>
>>def all_size_n_knickers(rqd_size, pieces):
>>     npieces = len(pieces)
>>     knicker_count = npieces ** rqd_size
>>     austen = [npieces ** (rqd_size-k-1) for k in xrange(rqd_size)]
>>     for i in xrange(knicker_count):
>>         knicker = [pieces[j] for j in [(i // d) % npieces for d in austen]]
>>         yield knicker
>>
>>for alist in all_size_n_knickers(4, 'abc'):
>>     print ''.join(alist)
>>
>>print
>>print list(all_size_n_knickers(2, [1, 42, 666]))
> 
> 
> Just testing out my ELCH JythonConsole :-)
> 
> def unint(i,symbols):
> 	res = []
> 	radix = len(symbols)
> 	while i:
> 		i,r = divmod(i,radix)
> 		res.append(symbols[r])
> 	return res[::-1]
> 
> start = int('10000',3)
> finish = int('20000',3)
> for i in range(start,finish):
> 	print ''.join(unint(i,'abc')[1:])
> 
> This makes me wonder why we still don't have something like the unint
> function above in the standard distribution.
> 
Because it's not what you'd call (or, at least, it's not what I'd call) 
universally required. As you have shown it is relatively easy to hack 
something supp when it's needed, so since it isn't something that's 
required by the majority it hasn't been added to the library.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list