• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Permutation Number

New Here ,
Jul 26, 2007 Jul 26, 2007

Copy link to clipboard

Copied

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?
TOPICS
Advanced techniques

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

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#'">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 30, 2007 Jul 30, 2007

Copy link to clipboard

Copied

oops will only work for a 4 digit number or 4 character word. Add in extra loops for more digits.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2007 Aug 05, 2007

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 08, 2007 Aug 08, 2007

Copy link to clipboard

Copied

anyone can help??

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 22, 2007 Aug 22, 2007

Copy link to clipboard

Copied

I still looking for someone can help..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 22, 2007 Aug 22, 2007

Copy link to clipboard

Copied

Store the results in a structure instead of an array. Structures don't allow duplicate keys.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 25, 2007 Aug 25, 2007

Copy link to clipboard

Copied

Has tested using sturcture but still have duplicate number?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 25, 2007 Aug 25, 2007

Copy link to clipboard

Copied

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#" />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2007 Aug 25, 2007

Copy link to clipboard

Copied

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:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 25, 2007 Aug 25, 2007

Copy link to clipboard

Copied

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)#">


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2007 Aug 25, 2007

Copy link to clipboard

Copied

cf_dev2,

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



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 25, 2007 Aug 25, 2007

Copy link to clipboard

Copied

We can simplify it even further by eliminating the array entirely



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 26, 2007 Aug 26, 2007

Copy link to clipboard

Copied

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.



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 26, 2007 Aug 26, 2007

Copy link to clipboard

Copied

LATEST
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).

--
Adam


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation