Possibility List Calculation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Karl Raams
    New Member
    • Jul 2011
    • 8

    Possibility List Calculation

    Hi Guys,

    I've got this unique situation that I've never come across before. What I want to do is create a piece of code that will add to a list all the possibilities based off of settings that have been set.

    What I currently have is a list of possible entries that looks somthing like this:
    1. S
    2. M
    3. T


    I also have an entry in the program where the user defines how many spaces there can be. For this example I'll use 4.

    What I want to be able to do is generate a list of all possible combinations bassed of of the number of entries in the list (the user has the ability to add or remove entries whenever they want) and the number of places that they enter.

    For the example information I have provided I want the list to show:
    1. SSSS
    2. SSSM
    3. SSST
    4. SSMM
    5. SSMT
    6. SSTT
    7. SSTM
    8. SMMM
    9. SMMT
    10. SMTT
    11. STTT
    12. MMMM
    13. MMMT
    14. MMTT
    15. MTTT
    16. TTTT


    I'm just not sure how to achieve this. I realise that I will need some sort of complex looping code to achieve this. Any suggestions or ideas on how to achieve this would be greatly appreciated. I have the details of what I currently am able to achieve below. I am using the programming language Clarion but I can also understand Visual Basic and JavaScript.

    The problem is that I can get the list to show:
    1. SSSS
    2. SSSM
    3. SSST
    4. SSM
    5. SST
    6. SM
    7. ST
    8. MMMM
    9. MMMT
    10. MMT
    11. MT
    12. TTTT


    Using the code below where the MinorListQ is the list containing the first list I've got above with the values of S, M, and T in it and the GeneticQ is the list I'm creating with all the probable values in it (the third list). The DMA:ActiveGenes contains the number 4 in the example I have given and anything with a # after it is a local variable that I've defined on the fly.

    Again I'm not sure if this code is useful to anyone I just need ideas on how to achieve this.

    Code:
      Loop MinorLoop1# = 1 to Records(MinorListQ) 
    
        Number# = DMA:ActiveGenes 
    
        clear(MinorListQ)
        MLQ:Number = MinorLoop1# 
        get(MinorListQ,MLQ:Number)
        if error(); message(error()).
        
        clear(GeneticQ)    
        loop Loop1# = 1 to Number#
          QGE:ActiveGenes = clip(QGE:ActiveGenes)&''&clip(MLQ:Gene)
        end
    
        add(GeneticQ)
    
        loop 
          Number# -= 1
          if Number# <= 0; break.
    
          MinorLoop2# = (MinorLoop1# + 1)
        
          clear(MinorListQ)
          MLQ:Number = MinorLoop1#
          get(MinorListQ,MLQ:Number)
          if error(); message(error()).
    
          clear(GeneticQ)
          
          clear(AGenes)
          
          loop Loop1# = 1 to Number#
            AGenes = clip(AGenes)&''&clip(MLQ:Gene)
          end
    
          loop Loop2# = MinorLoop2# to DMA:ActiveGenes 
            clear(MinorListQ)
            MLQ:Number = Loop2#
            get(MinorListQ,MLQ:Number)
            if error(); message(error()).
            QGE:ActiveGenes = clip(AGenes)&''&clip(MLQ:Gene)
            add(GeneticQ)
          end  
        end
      end

    Sorry for the long post. All suggestions and ideas are welcomed.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You could do something like this:
    Code:
    for a = 1 to 3
       for b = a to 3
          for c = b to 3
             for d = c to 3
                print a & b & c & d
             end for
          end for
       end for
    end for

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      By the way, result 7 in your post is wrong. It shouldn't be there. And for future reference, the concept is called a Combination with Repetition.

      Comment

      • Karl Raams
        New Member
        • Jul 2011
        • 8

        #4
        Thanks for pointing me in the right direction I was searching up possibility instead of combination formulas. When you say result 7 did you mean the first 7 (SSTM) or the second 7 (ST). I don't see what is wrong with either of them. Could you please explain.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Number 5 is SSMT and number 7 is SSTM. It's a repeat, one of them needs to go. I say number 7 because it doesn't follow the established pattern.

          Comment

          • Karl Raams
            New Member
            • Jul 2011
            • 8

            #6
            Oh... yes I see what you mean. Good spotting. Thank you a lot for your responses and the time you've spent to help answer my question. I've been able to make some headway and should be able to work out the final pieces to the puzzle now.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Good luck. Let us know how you get along.

              Comment

              Working...