all possible combinations

Anton Vredegoor anton.vredegoor at gmail.com
Thu Jul 28 06:38:39 EDT 2005


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.

Anton




More information about the Python-list mailing list