Possible combinations

Ask a Question related to Coldfusion - Getting Started, Design and Development.

  1. #1

    Default Possible combinations

    I have two lists: (A ) and (1,2,3). I need all possible combination of the two
    lists: Is there any way to loop throgh these combinations; A1, A2, A3, A12,
    A13,....... etc

    Any help is very appreciable.

    girmalemu Guest

  2. Similar Questions and Discussions

    1. digital combinations
      What digital combination is the equivalent to a 35mm with a 80 to 200 zoom lens.
    2. combinations
      "David Byrne" <dbyrne_commerce@yahoo.com> wrote in message news:20030804204911.18868.qmail@web40706.mail.yahoo.com... Hi David. This should do...
  3. #2

    Default Re: Possible combinations

    Something like this maybe? Arrays might be more efficient though.



    <cfset allCombinations = "">
    <cfset listOne = "A,B">
    <cfset listTwo = "1,2,3,12,13">

    <cfloop from="1" to="#listLen(listOne)#" index="x">
    <cfset outerValue = listGetAt(listOne, x)>
    <cfloop from="1" to="#listLen(listTwo)#" index="y">
    <cfset innerValue = listGetAt(listTwo, y)>
    <cfset allCombinations = listAppend(allCombinations, outerValue & innerValue)>
    </cfloop>
    </cfloop>
    <cfoutput>
    #allCombinations#
    </cfoutput>

    mxstu Guest

  4. #3

    Default Re: Possible combinations

    Thanx mxstu,

    I have also the same question in possible combinations of A with all numbers
    and all numbers themselves. Say I have (A ) and (1,2,3). Thus A, A-1, A-2,
    A-3, 1-1, 1-2, 1-3, 2-3.

    Thank you again


    johnclef Guest

  5. #4

    Default Re: Possible combinations

    What about 2-1, 2-2, 3-1,3-2 and 3-3?
    mxstu Guest

  6. #5

    Default Re: Possible combinations

    Yes, I mean that too.
    johnclef Guest

  7. #6

    Default Re: Possible combinations

    Something like this.... Note - I wouldn't use it for large lists because the
    total number of loops is the (number of elements in the list ^ 2). So if your
    second list contained 100 elements, CF will loop 10,000 times.



    <cfset listOne = "A">
    <cfset listTwo = "1,2,3">

    <cfset allCombinations = "">
    <!--- get letter number combinations --->
    <cfloop from="1" to="#listLen(listOne)#" index="x">
    <cfset outerValue = listGetAt(listOne, x)>
    <cfset allCombinations = listAppend(allCombinations, outerValue)>
    <cfloop from="1" to="#listLen(listTwo)#" index="y">
    <cfset innerValue = listGetAt(listTwo, y)>
    <cfset allCombinations = listAppend(allCombinations, outerValue &"-"&
    innerValue)>
    </cfloop>
    </cfloop>
    <cfloop from="1" to="#listLen(listTwo)#" index="x">
    <cfset outerValue = listGetAt(listTwo, x)>
    <cfloop from="1" to="#listLen(listTwo)#" index="y">
    <cfset innerValue = listGetAt(listTwo, y)>
    <cfset allCombinations = listAppend(allCombinations, outerValue &"-"&
    innerValue)>
    </cfloop>
    </cfloop>

    <cfoutput>
    #allCombinations#
    </cfoutput>

    mxstu Guest

  8. #7

    Default Re: Possible combinations

    mxstu,

    IT WORKS, THAT WAS GR8! IT IS EXACTLY WHAT I NEEDED.

    Thank you a lot.
    johnclef Guest

  9. #8

    Default Re: Possible combinations


    What about A-1-1, A-1-2, A-1-3, A-2-1,A-2-2,A-2-3,A-3-1,A-3-2,A-3-3, A-1-2-3, ...................?



    girmalemu Guest

  10. #9

    Default Re: Possible combinations

    <cfset listOne = "A">
    <cfset listTwo = "1,2,3">

    <cfset allCombinations = "">
    <!--- get letter number combinations --->
    <cfloop from="1" to="#listLen(listOne)#" index="x">
    <cfset outerValue = listGetAt(listOne, x)>
    <cfset allCombinations = listAppend(allCombinations, outerValue)>
    <cfloop from="1" to="#listLen(listTwo)#" index="y">
    <cfset innerValue = listGetAt(listTwo, y)>
    <cfset allCombinations = listAppend(allCombinations, outerValue &"-"&
    innerValue)>
    </cfloop>
    </cfloop>
    <cfloop from="1" to="#listLen(allCombinations)#" index="x">
    <cfset outerValue = listGetAt(allCombinations, x)>
    <cfloop from="1" to="#listLen(listTwo)#" index="y">
    <cfset innerValue = listGetAt(listTwo, y)>
    <cfset allCombinations = listAppend(allCombinations, outerValue &"-"&
    innerValue)>
    </cfloop>
    </cfloop>

    <cfoutput>
    #allCombinations#
    </cfoutput>

    Loop through allCombinations to get all the combinations ut redundant.

    A
    A-1
    A-2
    A-3
    A-1
    A-2
    A-3
    A-1-1
    A-1-2
    A-1-3
    A-2-1
    A-2-2
    A-2-3
    A-3-1
    A-3-2
    A-3-3


    What I want is:

    A
    A-1
    A-2
    A-3
    A-1-2
    A-1-3
    A-2-3




    girmalemu Guest

  11. #10

    Default Re: Possible combinations

    girmalemu,

    That last example was in reponse to johnclef's question. I believe he needs
    something slightly different than what you orginally requested, which is:

    I have two lists: (A ) and (1,2,3). I need all possible combination of the two
    lists: Is there any way to loop throgh these combinations; A1, A2, A3, A12,
    A13,....... etc

    Are you now looking for something different?

    mxstu Guest

  12. #11

    Default Re: Possible combinations

    Originally posted by: mxstu
    girmalemu,

    That last example was in reponse to johnclef's question. I believe he needs
    something slightly different than what you orginally requested, which is:

    I have two lists: (A ) and (1,2,3). I need all possible combination of the two
    lists: Is there any way to loop throgh these combinations; A1, A2, A3, A12,
    A13,....... etc

    Are you now looking for something different?

    Yes, What I want is:

    A
    A-1
    A-2
    A-3
    A-1-2
    A-1-3
    A-2-3



    girmalemu Guest

  13. #12

    Default Re: Possible combinations

    girmalemu,

    You should be able to do this by adding another loop. I adjusted the code to
    use the technique mentioned by Neculai on your other thread. It is cleaner.

    Note - As mentioned before, this would not be suitable for large lists, due to
    the total number of loops required.




    <cfset allCombinations = "">
    <cfset listOne = "A">
    <cfset listTwo = "1,2,3">

    <cfloop list="#listOne#" index="x">
    <cfset allCombinations = listAppend(allCombinations, x)>
    <cfloop list="#listTwo#" index="y">
    <cfset allCombinations = listAppend(allCombinations, x &"-"& y )>
    <cfloop list="#listTwo#" index="z">
    <cfset allCombinations = listAppend(allCombinations, x &"-"& y &"-"& z )>
    </cfloop>
    </cfloop>
    </cfloop>

    <cfoutput>
    #allCombinations#
    </cfoutput>

    mxstu Guest

  14. #13

    Default Re: Possible combinations

    Mxstu,

    Thank you for the propmt reply. But

    <cfset listOne = "A">
    <cfset listTwo = "1,2">

    gives me:

    A
    A-1
    A-1-1
    A-1-2
    A-2
    A-2-1
    A-2-2

    What I need is:

    A
    A-1
    A-2
    A-1-2

    The rest of the combination is redundant.

    Thanx!




    girmalemu Guest

  15. #14

    Default Re: Possible combinations

    Originally posted by: mxstu
    girmalemu,

    You should be able to do this by adding another loop. I adjusted the code to
    use the technique mentioned by Neculai on your other thread. It is cleaner.
    Although you may have to sort it if you want the elements in the same order you
    mentioned.

    Note - As mentioned before, this would not be suitable for large lists, due to
    the total number of loops required.




    Mxstu,

    Thank you for the propmt reply. But

    <cfset listOne = "A">
    <cfset listTwo = "1,2,n">

    gives me:

    A
    A-1
    A-1-1
    A-1-2
    A-2
    A-2-1
    A-2-2

    What I need is:

    A
    A-1
    A-2
    A-1-2
    A-1-2-n


    Thanx!




    girmalemu Guest

  16. #15

    Default Re: Possible combinations

    What I need is:
    A
    A-1
    A-2
    A-1-2
    A-1-2-n


    I'm really not sure what the logic here is. Once you define your rules for
    what values you want returned it should be simple to modify the sample code to
    achieve that.


    mxstu Guest

  17. #16

    Default Re: Possible combinations

    Originally posted by: mxstu
    girmalemu,

    You should be able to do this by adding another loop. I adjusted the code to
    use the technique mentioned by Neculai on your other thread. It is cleaner.
    Although you may have to sort it if you want the elements in the same order you
    mentioned.

    Note - As mentioned before, this would not be suitable for large lists, due to
    the total number of loops required.




    Mxstu,

    Thank you very much for your propmt reply. But

    <cfset listOne = "A">
    <cfset listTwo = "1,2">

    Produces:
    A
    A-1
    A-1-1
    A-1-2
    A-2
    A-2-1
    A-2-2

    Here is the situation:

    <cfset listOne = "A,[n]">
    <cfset listTwo = "1,2,[n]">

    The lists are dynamic, [n] ranges from 1 to 12.

    What I need is:

    A
    A-1
    A-2
    A-[n]
    A-1-2
    A-1-2-[n]

    Thanks a lot for your help!



    girmalemu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139