# Ray's Friday Puzzler: Listing All Possibilities Of A Set

Posted April 27, 2007 at 1:35 PM by Ben Nadel

Tags: ColdFusion

I finally got a chance to work on Ray's puzzler for April 27, 2007. It builds the result set by viewing the array as a table that has rows (set values) and columns (characters within a value). When you look at this way, you can build the array using a simple nested loop. The complicated part is using this RxC (row by column) index to figure out which character we are supposed to be using.

After analyzing the possible outcomes you will see that there are N^X possible values where N is the number of characters in the passed in set and X is the length of the each resultant item. This becomes our number of rows. Then, of course, the number of character columns is equal to the length of the set items (also passed id).

The alternating is a bit trickier to see. The last column has a new value for every row. The second to last column has a new value for every N rows (where N is the number of available characters). The third to last column has a new value for every N * N rows since it can only alternate after the second to last row has alternated.

You might begin to see the pattern is that a given column value alternates based on the length of the sets (X), the current column (C) and the number of characters (N) using this rule:

N^(X - C)

Once we know that, we can figure out which letter we are looking at based on the number of times our sequence has repeated and the current row. That equation is a bit complicated, so see the code below:

• <cffunction
• name="AllSets"
• access="public"
• returntype="array"
• output="false"
• hint="Given the list of characters and the desired length, an array of all possible value combinations will be returned.">
•
• <!--- Define arguments. --->
• <cfargument
• name="Values"
• type="string"
• required="true"
• hint="A comma delimited list of values."
• />
•
• <cfargument
• name="Length"
• type="numeric"
• required="true"
• hint="The length of the resultant set values."
• />
•
• <!--- Define the local scope. --->
• <cfset var LOCAL = StructNew() />
•
•
• <!---
• Check to see if we have a valid set and a valid length.
• We can only return resultant value if we have a value
• set and length that is bigger than zero.
• --->
• <cfif (
• (NOT Len( ARGUMENTS.Values )) OR
• (ARGUMENTS.Length LTE 0)
• )>
•
• <!---
• We have an invalid set or target length. To work
• with this, just return an empty array.
• --->
• <cfreturn ArrayNew( 1 ) />
•
•
• <!---
• Check to see if the length is one. If it's just one,
• then we don't have to do any work; just convert the
• set to an array.
• --->
• <cfelseif (ARGUMENTS.Length EQ 1)>
•
• <!--- Create and return an array from the list. --->
• <cfreturn ListToArray(
• ARGUMENTS.Values
• ) />
•
• </cfif>
•
•
• <!---
• ASSERT: At this pointer, we know that we definitely
• have a valid set of values and a valid target length
• that is greater than one (this will actually require
• value combinations).
• --->
•
•
• <!---
• Convert the set to an array so that we can more eaily
• access each letter.
• --->
• <cfset LOCAL.Values = ListToArray(
• ARGUMENTS.Values
• ) />
•
•
• <!--- Create an array to hold the set values. --->
• <cfset LOCAL.Set = ArrayNew( 1 ) />
•
•
• <!---
• Loop over the rows to start populating each record.
• We are going to be building one row at a time by
• looping over each possible character. We know how
• many records we have since each letter can be in
• each spot.
• --->
• <cfloop
• index="LOCAL.RowIndex"
• from="1"
• to="#(ListLen( ARGUMENTS.Values ) ^ ARGUMENTS.Length)#"
• step="1">
•
• <!---
• Create a set value that will be added to the set
• we are building. We are going to build the value
• out of the set first to make the code more readable.
• --->
• <cfset LOCAL.Value = "" />
•
• <!---
• Loop over each character in the string. We know
• how many characters we need based on the length
• that was passed in.
• --->
• <cfloop
• index="LOCAL.ColumnIndex"
• from="1"
• to="#ARGUMENTS.Length#"
• step="1">
•
• <!---
• Get the MOD Toggle index for this column. Each
• column value toggles (switches to the next
• character) based on a MOD index.
• --->
• <cfset LOCAL.ModIndex = (
• ArrayLen( LOCAL.Values ) ^
• (ARGUMENTS.Length - LOCAL.ColumnIndex)
• ) />
•
•
• <!---
• Get the bucket that we are in. By bucket, I mean
• which "iteration" are we in, in which this value
• is different form the previous letter.
• --->
• <cfset LOCAL.Bucket = Ceiling(
• LOCAL.RowIndex / LOCAL.ModIndex
• ) />
•
• <!---
• Once we have the bucket, we can use that and the
• number of available values to figure out which
• letter we should be pointing to.
• --->
• <cfset LOCAL.ValueIndex = (((LOCAL.Bucket - 1) MOD ArrayLen( LOCAL.Values )) + 1) />
•
•
• <!---
• Get the new value character out of our working
• set based on the value index we just calculated.
• --->
• <cfset LOCAL.Value = (
• LOCAL.Value &
• LOCAL.Values[ LOCAL.ValueIndex ]
• ) />
•
• </cfloop>
•
•
• <!--- Append the value to the results array. --->
• <cfset ArrayAppend(
• LOCAL.Set,
• LOCAL.Value
• ) />
•
• </cfloop>
•
•
• <!--- Return the results array. --->
• <cfreturn LOCAL.Set />
• </cffunction>

This uses two loops and no recursion, but is certainly a bit verbose. And now, to test; running this:

• <cfset arrSet = AllSets(
• "A,B,C",
• 3
• ) />
•
• <cfdump
• var="#arrSet#"
• label="Ray's Friday Puzzler - All Sets"
• />

... we get the following CFDump output:

I have color coded the alternating rows to help see where things are changing.

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

Apr 27, 2007 at 2:16 PM // reply »

Nice solution. I was working on something very similar by using the exponent to determine total possibilities and two loops, but I got a little stuck towards the end... I'm glad you solved it this way so that I can see where I went wrong.

Dan

Apr 27, 2007 at 2:26 PM // reply »

@Dan,

Thanks. Yeah, that MOD equation was driving me CRAZY for a while. I kept getting the sequence "1,2,0,1,2,0" and I wanted "1,2,3,1,2,3"!!!!

Jul 25, 2008 at 12:35 PM // reply »

Very elegant!
Unfortunately, it crashes before finishing if the armuments are too large. I'm trying to find a solution for 7 values with a length of 8 (~5.7 million possibilities).

Dec 31, 2008 at 6:15 PM // reply »

I was looking for a way that would allow me to calculate all of the possible hex colors. However, as chuck alluded to this would result in a VERY large number of combinations. Any thoughts on such a task 16 character list by 6 characters long? I'll let you know if i have any luck...

By the way, your blog is full of practical, easy to follow examples that have saved me countless hours. Thanks for all the tips and tricks that you take the time to share with other developers.

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 11, 2014 at 6:20 PM
Reading In File Data One Line At A Time Using ColdFusion's CFLoop Tag Or Java's LineNumberReader
Thanks Ben for you very interesting articles on handling large files. As I am not a programmer, I am quite insecure whether my issue is directly related to your description or not. My problem is, tha ... read »
Mar 11, 2014 at 12:21 PM
Compound Transclusion Prevented In AngularJS 1.2
Yeah this hit our team too and locked us as 1.1.5. It seemed like an irresponsible unilateral decision, but our graver concern is the future decisions the Angular team will make with other fundamenta ... read »
Mar 11, 2014 at 2:00 AM
ColdFusion, jQuery, And "AJAX" File Upload Demo
Mar 10, 2014 at 8:24 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Steven, the action property is not something from AngularJS but rather part of the concept that Ben is describing in his blogpost here. it is a custom added property, which is read by the requestCo ... read »
Mar 10, 2014 at 2:03 PM
Nested Views, Routing, And Deep Linking With AngularJS
Where is the angular documentation for 'action:' it is not here: http://docs.angularjs.org/api/ngRoute/provider/ \$routeProvider Thanks. ... read »
Mar 10, 2014 at 12:06 PM
Using Track-By With ngRepeat In AngularJS 1.2
I was hoping that this will work with pagination using ng-repeat. My use case scenario is that I have an images object. images[0] = [im1,im2,im3] // First Page images[1] = [im4, im5, im6] // Second ... read »
Mar 9, 2014 at 6:11 PM
For Better Security Use HtmlEditFormat() In Conjunction With JSStringFormat() In ColdFusion
It looks like htmleditformat() will be deprecated in CF 11 https://wikidocs.adobe.com/wiki/display/coldfusionen/New +in+ColdFusion ... read »
Mar 9, 2014 at 10:55 AM
\$.stop() vs. \$.finish() In jQuery Animations
Nice feature! Thanks for sharing. :) Good for when you are making a 100% AJAX controlled site. ... read »