Jump to content

Recommended Posts

Posted

Hi,

I've this topic which deals with something similar but not exactly the same... As my question is a bit more difficult...

I'd like to know how to get all the possible answer results of a an online survey.

Ex:

Question1 - 1/2/3

Question2 - 1/2

Question3 - 1/2

All possible permutations of the above survey will be 3*2*2 (12) that i know...

What I need is to output all the possible results into a table...

Ex:

__________________

1,1,1 | 2,1,1 | 3,1,1 |

1,1,2 | 2,1,2 | 3,1,2 |

1,2,1 | 2,2,1 | 3,2,1 |

1,2,2 | 2,2,2 | 3,2,2 |

__________________|

I know i should use some recursive method for that... any idea ?!

Any help will be appreciated.

Thanks in advance,

SK.

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Posted

No it shouldn't ... the math problem is not even there ... The issue is how to loop through and create the proper output !

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Posted (edited)

Good luck wrapping your head around this one.

Code:

;Number of rows = Number of questions
Dim $aQuestions[3]

;Assign each question a number of possible answers
$aQuestions[0] = 3
$aQuestions[1] = 2
$aQuestions[2] = 2

_Recurse($aQuestions)

Func _Recurse(ByRef $aArray, $iIndex=0, $sString ='')
    For $Y = 1 to $aArray[$iIndex]
        $newString = $sString & $Y

        ;Dump line at deepest recursion depth
        If $iIndex = Ubound($aArray)-1 Then
            ConsoleWrite($newString & @CRLF)
        Else
            $newString &= ','
            _Recurse($aArray, $iIndex+1, $newString)
        EndIf
    Next
EndFunc

Output:

1,1,1
1,1,2
1,2,1
1,2,2
2,1,1
2,1,2
2,2,1
2,2,2
3,1,1
3,1,2
3,2,1
3,2,2
Edited by weaponx
Posted (edited)

Well... solved it already... forgot to update in here as i was needing it for a PHP project i'm working on ...

here is what i came up with:

$rules = 1; // At least 1 rule.
    // First, calculate how many rules do we need to create.
    foreach($all_question as $question) {
        $rules *= $question->answer_count;
    }
    // Parse all rules.
    $ruleArr = array();
    $ruleArrStr = array();
    $lastQ = count($all_question)-1;
    $lastC = $lastQ;
    for ($i = 0; $i < $rules; $i++){
        // Set the array to work with.
        for ($c = 0; $c < count($all_question); $c++){
            $ruleArr[$i][$c] = 1;
        }
        // Prep the first search string.
        $str = implode(', ', $ruleArr[$i]);
        // Run the loops.
        while(array_search($str, $ruleArrStr)!==false){
            while($ruleArr[$i][$lastC] == $all_question[$lastC]->answer_count){
                $ruleArr[$i][$lastC] = 1;
                $lastC-=1;
                if($lastC<0){break 2;}
            }
            $ruleArr[$i][$lastC] += 1;
            $lastC = $lastQ;
            // Keep the loop running if needed.
            $str = implode(', ', $ruleArr[$i]);
        }
        // Save these strings that fit.
        $ruleArrStr[$i] = implode(', ', $ruleArr[$i]);
    
    }

I have to admit, yours does look a hell lot nicer :D

Probably also works faster... less searching and so on...

Maybe i'll translate it to php anyways...

Thanks man !!!

Edited by Armand

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...