PDA

Click to See Complete Forum and Search --> : Unique combinations problem


trip85
Jan 11th, 2005, 09:55 AM
Hello everyone-

I write software where i have to display catogories on a screen (menus on a tv screen) that a user can navigate through. Currently i am implemnting new functionality that involves subscription based categories, so that only certain people who are entitled can view a particular category. There can be any number of subscription categories configured and the people should only see on the screen the cateogries that they are entitled to enter. So i need to create a screen for each unigue possible combination of these subscription cateogires on the screen.

So if i have 5 subscription categories configured, that means that i have (2 ^ 5) - 1 or 31 unique combinations of categories. If the category IDs are 10, 20, 30, 40, and 50 then these would be the 31 unique combinations:

(10,,,,)
(10,20,,,)
(10,,30,,)
(10,,,40,)
(10,,,,50)
(10,20,,,)
(10,20,30,,)
(10,20,,40,)
(10,20,,,50)
(10,20,30,40,)
(10,20,30,,50)
(10,20,30,40,)
(10,20,30,40,50)
(10,,30,40,)
(10,,30,40,50)
(10,,,40,50)
(,20,,,)
(,20,30,,)
(,20,,40,)
(,20,,,50)
(,20,30,40,)
(,20,30,,50)
(,20,30,40,50)
(,20,,40,50)
(,,30,,)
(,,30,40,)
(,,30,,50)
(,,30,40,50)
(,,,40,)
(,,,40,50)
(,,,,50)

The problem that i am having is how to figure out these combinations through code. For some reason my mind is drawing a complete blank today. I would greatly appreciate any help with this problem.

Thanks,
Brian

twanvl
Jan 11th, 2005, 12:21 PM
The idea here is to create an array of permutations, this array will double in size:

permutations = empty array
for each v in value
copy_of_permutations = permutations
for each e in copy_of_permutations
add the value v to the permutation e
permutations = permutations merged with copy_of_permutations