14 Replies Latest reply on Aug 26, 2007 4:13 AM by Adam Cameron

# Permutation Number

How do i write a CF script can do searching for the permutation number:

Example search for 4 digit number like: 1234

Result:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

Anyone can help?
• ###### 1. Re: Permutation Number
Try this...

<cfset init = "12345">
<cfscript>
permArray = arrayNew(1);
cnt = 0;
for(a=1; a lte len(init); a = a + 1){
aNum = Mid(init,a,1);
aTemp = RemoveChars(init,a,1);
for(b=1; b lte len(aTemp); b = b + 1){
bNum = Mid(aTemp,b,1);
bTemp = RemoveChars(aTemp,b,1);
for(c=1; c lte len(bTemp); c = c + 1){
cNum = Mid(bTemp,c,1);
cTemp = RemoveChars(bTemp,c,1);
for(d=1; d lte len(cTemp); d = d + 1){
dNum = Mid(cTemp,d,1);
dTemp = RemoveChars(cTemp,d,1);
cnt = cnt + 1;
permArray[cnt] = aNum & bNum & cNum & dNum;
}
}
}
}
</cfscript>
<cfdump var="#permArray#" label="Permutations for '#init#'">
• ###### 2. Re: Permutation Number
oops will only work for a 4 digit number or 4 character word. Add in extra loops for more digits.
• ###### 3. Re: Permutation Number
Thank a lot, the script is work, but if i searh for number like "8000" the result will repeat few time, any method to show the number only one time for one number?
• ###### 4. Re: Permutation Number
anyone can help??
• ###### 5. Re: Permutation Number
I still looking for someone can help..
• ###### 6. Re: Permutation Number
Store the results in a structure instead of an array. Structures don't allow duplicate keys.
• ###### 7. Re: Permutation Number
Has tested using sturcture but still have duplicate number?
• ###### 8. Re: Permutation Number
I don't see how that's possible. Structures don't allow duplicate keys.

Notice the output contains only 1 value?

<cfset myStruct = StructNew() />
<cfset myStruct["8000"] = "a" />
<cfset myStruct["8000"] = "b" />
<cfset myStruct["8000"] = "c" />

<cfdump var="#myStruct#" />
• ###### 9. Re: Permutation Number
Cybersabah wrote:
Thank a lot, the script is work, but if i searh for number like "8000" the result will repeat few time, any method to show the number only one time for one number?

RedBeard1973 has already done a good job. Just include an extension to remove duplicates. An example follows:

• ###### 10. Permutation Number
I imagine that would work, but if you use a structure you don't need to loop through the array looking for dupes, because there won't be any.

If you replace
permArray[cnt] = aNum & bNum & cNum & dNum;

with
permStructure[aNum & bNum & cNum & dNum] = aNum &
bNum & cNum & dNum;

At the end use StructKeyArray to return the permutations

<cfdump var="#StructKeyArray(permStructure)#">

• ###### 11. Re: Permutation Number
cf_dev2,

I played with your method, too. It is the cleverer and, I should say, the better code. An example for Cybersabah:

• ###### 12. Re: Permutation Number
We can simplify it even further by eliminating the array entirely

• ###### 13. Re: Permutation Number
Cf_dev2 wrote:
We can simplify it even further by eliminating the array entirely

Naturally. However, I am in favour of keeping the array, that is, of leaving RedBeard1973' s code intact. PermArray has more scope for extension than permStruct. For example, RedBeard1973' s code could be the basis of a function from which one might wish to know the total number of permutations, how many duplicates there are, and so on.

• ###### 14. Re: Permutation Number
This seems to work (attached), is more scalable (it's not restricted in the input-string length), and is encapsulated in a function for portability.

Indeed other than the overhead of the <cffunction>/<cfargument> tags, it's fewer lines of code, too (not that that matters, really).

--