View Full Version : Generating Combo Lists

06-06-2007, 10:35 PM

Lets say I have an array:
$fruitarr = array("apple", "banana", "orange", "lemon", "lime", "strawberry");

And I need to generate a list of smoothies over a period of 8 weeks. Each week only two of the fruits can be used together at a time. So for example, the choices for week 1 would be:

apple, banana
orange, lemon
lime, strawberry

week 2 would be:

banana, orange
lemon, lime
strawberry, apple

and so on....not repeating together until each has been paired up at least once. (Since there are 8 weeks, the combos for later weeks will resemble the combos of first few weeks)

How do I generate these combo lists?

Also lets say I have an odd number of fruits in my array:
$fruitarr = array("apple", "banana", "orange", "lemon", "lime");

week 1 would be:
apple, banana
orange, lemon
lime, apple <- first gets repeated

week 2:
banana, orange,
lemon, lime,
apple, banana <- first gets repeated

How could I generate these combos? (for odd numbers of fruits)

Thanks. I hope this smoothie example makes my problem easier to conceptualize.

If anyone could help I would greatly appreciate it.

06-07-2007, 03:36 AM
This is err... more complicated than I originally perceived it.
Give me a few minutes, I'll try to pump something out for it.

k, I'll pseudo code this for you, since I don't know your php version and what nots:

Set $fileName to 'week.txt'; // This holds the current week information
Set $fruitArray to array of fruits; // Number is irrelivent (depending on how extensive the checking is, may result in a 1, 1 combination if only one item exists)
Get the current week in the year
IF Not Exists $fileName THEN
CALL func writeData with (1, current week in year);
End IF
Set $infoIn to fileContents;
Set $cboWeek to $infoIn[0];
Set $recWeek to $infoIn[1];
Set $combos to newArray;
WHILE $i < $cboWeek - 1
Set $newFruitArray to deQueue($fruitArray);
Set $fruitArray to enQueue($newFruitArray);
FOR $i < size of $fruitArray $i += 2
IF NOT empty $fruitArray itemAt $i + 1 THEN
Set $matchWith to $fruitArray itemAt $i + 1
Set $matchWith to 0
enQueue $combos value newArray($fruitArray itemAt $i, $fruitArray itemAt $matchWith)
// At this point, $combos holds the arrays you are looking for
CALL func writeData WITH (); // No arguments

writeData($forceCboWeek, $forceRecWeek)

Get $fileName, $cboWeek, $recWeek, $curWeek;
Set $writeInfo to '';
IF NOT empty $forceCboWeek AND NOT empty $forceRecWeek THEN
Set $writeInfo to '$forceCboWeek\r\n$forceRecWeek';
IF $recWeek < $curWeek THEN
Set $writeInfo to '($cboWeek + $curWeek - $recWeek)\r\n$curWeek';
IF NOT empty $writeInfo THEN
Write $writeInfo to $fileName

Don't want to code this all for you, what would you learn then ;)
Some hints for you though since I'm not completely cruel

WriteData method is overloaded. In php, thats simply giving it parameters which are defaulted - function func($param1 = '', $param2 = ''...)
You must know how to read/write files in php. This is left to your descretions as there are multiple ways to perform this depending on your version of php.
If you are new to queues:

enQueue means add item to the back
deQueue means remove item from the front

Any trouble let me know. I'm a little rusty with the php, but I think this is probably the most efficient method of using a file controlled method of handling the arrays properly. If you want, you can easily create the $fruitArray out of file items as well, but I'll leave that to you :D

06-07-2007, 11:02 AM
Thanks....I'll give it a try.