Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2006
    Thanked 0 Times in 0 Posts

    Generating Combo Lists


    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    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);
        END WHILE
        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
            END IF
            enQueue $combos value newArray($fruitArray itemAt $i, $fruitArray itemAt $matchWith)
        END FOR
        // 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';
            END IF
        END IF
        IF NOT empty $writeInfo THEN
            Write $writeInfo to $fileName
        END IF
    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
    Last edited by Fou-Lu; 06-07-2007 at 04:09 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Dec 2006
    Thanked 0 Times in 0 Posts
    Thanks....I'll give it a try.


    Posting Permissions

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