Hi, Ken,

I recently had to build a permutation generator to generate all the unique 
combinations of a set of 

data where (just as in your case) order was not a factor.  So given the data 
set "A,B,C", I needed 

to get out:

A,B,C
A,B
B,C
A,C
A
B
C

I looked at my code and was able to modify it to (hopefully) suit your purpose. 
 It requires using recursion, so most of the work is done in a CFC function.

Here's the code for the CFC:

<cfcomponent displayname="Permutations" hint="I generate the permutations." 
 output="false">

        <cffunction name="generatePermutations" output="true" 
returntype="string" hint="I return a list of all possible combinations of a 
list of items where the order of the items is constant but one or more items 
can be missing">
        <cfargument name="starting_list" type="string" required="yes" hint="The 
starting list of items.  The list will grow smaller via recursion." />
        <cfargument name="permutation_list" type="string" required="yes" 
hint="The current list of permutations, which will grow longer." />
        <cfargument name="start_delimiter" type="string" required="yes" 
hint="The list delimiter." />
        <cfargument name="perm_delimiter" type="string" required="yes" 
hint="The delimiter for the final list.  Should be different from 
start_delimiter" />
        <cfset var temp= "">
        <cfset var p_list= arguments.permutation_list>

        <!---Determine if the full list of items is already in the permutations 
list.  If not, add it--->
        <cfif Not 
ListFind(p_list,arguments.starting_list,arguments.perm_delimiter) AND 
ListLen(arguments.starting_list,",") EQ 6>
                <cfset p_list= 
ListAppend(p_list,arguments.starting_list,arguments.perm_delimiter)>
        </cfif>

        <!---If starting_list length is not 6, loop and call the function for 
each iteration of the loop--->
        <cfif ListLen(arguments.starting_list,arguments.start_delimiter) GT 6>
                <cfloop index="gone" from="1" 
to="#ListLen(arguments.starting_list,arguments.start_delimiter)#">
                        <cfset temp= 
ListDeleteAt(arguments.starting_list,gone,arguments.start_delimiter)>
                        <cfinvoke component="Permutations" 
method="generatePermutations" returnvariable="p_list" starting_list="#temp#" 
permutation_list="#p_list#" start_delimiter="#arguments.start_delimiter#" 
perm_delimiter="#arguments.perm_delimiter#"></cfinvoke>
                </cfloop>
        </cfif>

        <cfreturn p_list />

        </cffunction>

</cfcomponent>


....And here's the code for a page that you can use to test it:

<cfsetting requesttimeout="600">
<cfoutput>

        <cfset inputValues1= '1,2,3,4,5,6,7,'>
        <cfset inputValues2= '1,2,3,4,5,6,7,8,9,10,11,12'>
        
        <p>
                inputValues1 is:  #inputValues1#
        </p>
        
        <cfinvoke component="Permutations" method="generatePermutations" 
starting_list="#inputValues1#" permutation_list="" start_delimiter="," 
perm_delimiter="|" returnvariable="fullPermutationList"></cfinvoke>
        
        <p>List the permutations:</p>
        <ul>
          <cfloop index="combo" list="#fullPermutationList#" delimiters="|">
             <li>#combo#</li>
          </cfloop>
        </ul>

</cfoutput>


....It can render your "1,2,3,4,5,6,7" example pretty quickly, but it takes a 
pretty long time to do something larger like a 12-item set (hence the 
<cfsetting> tag to increase the timeout), and I didn't have the time to figure 
out how much time it would take to process a full 20-item list.

Maybe there's a better way to do it, but hopefully this at least provides a 
starting point.

--
Brian Swartzfager
[EMAIL PROTECTED]



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Enterprise web applications, build robust, secure 
scalable apps today - Try it now ColdFusion Today
ColdFusion 8 beta - Build next generation apps

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:289519
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to