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

Loop 8 characters

Participant ,
Aug 27, 2015 Aug 27, 2015

How could I loop through all possible rearrangement combinations of these 8 characters? LLLMMRRR

I need it to be output with trace command and numbered if possible.

TOPICS
ActionScript
507
Translate
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 27, 2015 Aug 27, 2015

is each L a different character?  ie, if you only had LLL would there be one or 6 displays?

Translate
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
Participant ,
Aug 27, 2015 Aug 27, 2015

I have 3 L's 2 M's and 3 R's in one arrangement like the one you see above.  I want them to be rearranged in deferent order and I need to see all possible combination in the output through trace command.

Translate
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 27, 2015 Aug 27, 2015

again, are different combinations that look the same, different?  if so, you'll see LLLMMRRR listed 72 times.

Translate
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
Participant ,
Aug 27, 2015 Aug 27, 2015

I want to output all possible combination since I have to go through each one by one.

Translate
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 27, 2015 Aug 27, 2015

var i:int;

var s:String = 'LLLMMRRR';

var permA:Array = [];

permF(s,'');

trace(permA);

function permF(s: String, pre: String): void {

    if(s.length < 1){

        permA.push(pre);

    } else {

        for(i = 0; i < s.length; i++){

            permF(s.substr(0, i) + s.substr(i + 1), pre + s.charAt(i));

        }

    }

}

p.s. if you want combinations (i never got an answer from you that i could understand), sort permA and loop through its elements checking for duplicates:

function removeDupsF(a:Array):Array{

    a.sort();

    for(var j:int=a.length-1;j>0;j--){

        if(a==a[j-1]){

            a.splice(j,1);

        }

    }

    return a;

}

Translate
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
Participant ,
Aug 27, 2015 Aug 27, 2015

I only get one output LLLMMRRR

Translate
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 27, 2015 Aug 27, 2015

that's interesting.  the problem is i.

use:

//var i:int; // commented out

var s:String = 'LLLMMRRR';

var permA:Array = [];

permF(s,'');

trace(permA);

function permF(s: String, pre: String): void {

    if(s.length < 1){

        permA.push(pre);

    } else {

        for(var i:int = 0; i < s.length; i++){  // <-i is local of permF and retains its value during during function calls

            permF(s.substr(0, i) + s.substr(i + 1), pre + s.charAt(i));

        }

    }

}

and please mark correct and helpful responses.  i see you still have not done that in your collision thread.

Translate
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
Participant ,
Aug 28, 2015 Aug 28, 2015
LATEST

This one worked...ish, I got all the possible combinations (I think) but

they were multiplied by 72. so I got 72 LLLMMRRR and then I got

72 LLLMRMRR... and so on and so forth.

Translate
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