Author Topic: Math/Statistics - All possible combinations of a survey's answers :(  (Read 793 times)

0 Members and 1 Guest are viewing this topic.

Offline shlomikalfaTopic starter

  • Enthusiast
  • Posts: 177
    • View Profile
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.

Offline shlomikalfaTopic starter

  • Enthusiast
  • Posts: 177
    • View Profile
Re: Math/Statistics - All possible combinations of a survey's answers :(
« Reply #1 on: February 05, 2010, 10:31:46 AM »
no one ?!

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Math/Statistics - All possible combinations of a survey's answers :(
« Reply #2 on: February 05, 2010, 11:13:11 AM »
post some more information regarding your database structure
Developer from Belgium, Vlaams-Brabant

Offline shlomikalfaTopic starter

  • Enthusiast
  • Posts: 177
    • View Profile
Re: Math/Statistics - All possible combinations of a survey's answers :(
« Reply #3 on: February 05, 2010, 11:31:51 AM »
Well... there isn't much more to it...

I have an obj with question in it: $all_question
Inside, it has the questions... each of them has several number of answers... (different count).

I want to be able to output all the possible combinations of $all_question.

I can't explain it any better than I already did i think ... :(

This is what i have so far:

	
$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;
	
	
}
	
	

	
	
$qn 0;
	
	
$str implode(', '$ruleArr[$i]);
	
	
while(
array_search($str$ruleArrStr)!==false){
	
	
	
while(
$ruleArr[$i][$lastC] == $all_question[$lastC]->answer_count){
	
	
	
	
$lastC-=1;
	
	
	
	
if(
$lastC==0){break 2;}
	
	
	
}
	
	
	
$ruleArr[$i][$lastC] += 1;
	
	
	
$lastC $lastQ;
	
	
	
	
	

	
	
	
$str implode(', '$ruleArr[$i]);
	
	
	
echo 
'search: '.$str;
	
	
}

	
	
$ruleArrStr[$i] = implode(', '$ruleArr[$i]);
	

	
}
	
echo 
"<br /><br />";
	
print_r($ruleArrStr);


It's not working ofc....

I'm trying to move backwards... like:
1,1,1,1
1,1,1,2
1,1,1,3
1,1,2,1
1,1,2,2
1,1,2,3
........
« Last Edit: February 05, 2010, 11:36:04 AM by shlomikalfa »

Offline kathas

  • Enthusiast
  • Posts: 109
    • View Profile
    • Greek Job Search Engine
Re: Math/Statistics - All possible combinations of a survey's answers :(
« Reply #4 on: February 10, 2010, 02:25:18 PM »


$n 
count($all_question)-1;
$answers = array();
$answer array_fill(0,$n,1);
$change $n;

while (
$change || $answer[$change]<$all_question[$change]->answer_count)
{

if (
$answer[$change]<$all_question[$change]->answer_count)
{
$answer[$change]++;
$answers[] = implode($answer,',');
$change $n;
}
else
{
$answer[$change]=1;
$change--;
}


}



I tested it with this output.
The first question has 3 answers
second has only 1
third 2
4th 3

Code: [Select]
Array
(
    [0] => 1,1,1,1
    [1] => 1,1,1,2
    [2] => 1,1,1,3
    [3] => 1,1,2,1
    [4] => 1,1,2,2
    [5] => 1,1,2,3
    [6] => 2,1,1,1
    [7] => 2,1,1,2
    [8] => 2,1,1,3
    [9] => 2,1,2,1
    [10] => 2,1,2,2
    [11] => 2,1,2,3
    [12] => 3,1,1,1
    [13] => 3,1,1,2
    [14] => 3,1,1,3
    [15] => 3,1,2,1
    [16] => 3,1,2,2
    [17] => 3,1,2,3
)

Greek Job Search Engine - Seriously Greek