Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Generating Combo Lists

    Hi,

    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
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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:
    Code:
        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
            ELSE
                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
    STOP
    writeData($forceCboWeek, $forceRecWeek)
    Code:
        Get $fileName, $cboWeek, $recWeek, $curWeek;
        Set $writeInfo to '';
        IF NOT empty $forceCboWeek AND NOT empty $forceRecWeek THEN
            Set $writeInfo to '$forceCboWeek\r\n$forceRecWeek';
        ELSE
            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
    STOP
    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 03:09 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    18
    Thanks
    2
    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
    •