Combinations

Here’s a quick and simple way to generate combinations (Knuth):

t = #balls to pick
n = #balls in total
pos = range(0, t)+[n]+[0]
j = 0

while(j < t):
	j = 0
	while(pos[j]+1 == pos[j+1]):
		pos[j] = j
		j += 1
	pos[j] += 1
	f(pos)

I use this for a decoding algorithm to create Hamming balls. It works like a charm.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s