www.webdeveloper.com
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2006
    Location
    On the web, working on my website
    Posts
    181

    Help with Script

    Good evening,

    I am trying to figure out how to make the following possible.

    I have 8 rooms and 50 people labeled as such:

    H3, A1, S1, R4, I2, T1, C2, K1, V2, E2, W2, S2, P1, V1, N1, Q1, R3, C3, M2, W3, E1, L2, D1, X2, L3, R2, G1, H2, U1, M1, Q2, N2, U2, L1, J1, I1, R1, Q3, J2, T2, E3, X1, W4, W1, C1, P2, I3, A2, B1, and H1.

    What I am trying to do is to randomly place these 50 people into 8 different rooms (with a max of 7 in each room). The trick is to try not having the same letter in the same room. Would any of you be able to help me with this? I have attached a picture of what I am trying to attain.

    I have been wrapping my brain around this idea all day, and cannot find a way to begin. I know this is possible, but I have no idea. I look forward to hearing your responses.
    Attached Images

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,041
    There's probably a more elegant way, but here's what I came up with. (I'll leave it to you to output the $assignments array in the desired HTML format or whatever you want.)
    PHP Code:
    <?php
    $people 
    = array('H3''A1''S1''R4''I2''T1''C2''K1''V2''E2',
                    
    'W2''S2''P1''V1''N1''Q1''R3''C3''M2''W3',
                    
    'E1''L2''D1''X2''L3''R2''G1''H2''U1''M1',
                    
    'Q2''N2''U2''L1''J1''I1''R1''Q3''J2''T2',
                    
    'E3''X1''W4''W1''C1''P2''I3''A2''B1''H1');
    $letterGroups = array();
    foreach(
    $people as $person) {
        
    $letterGroups[substr($person01)][] = $person;
    }
    $rooms range(1,8);
    $assignments = array();
    foreach(
    $letterGroups as $group) {
        
    shuffle($rooms);
        
    shuffle($group);
        foreach(
    $rooms as $room) {
            if( ! isset(
    $assignments[$room]) or count($assignments[$room]) < 7) {
                
    $assignments[$room][] = array_pop($group);
                if(
    count($group) == 0) {
                    break;
                }
            }
        }
    }
    ksort($assignments);
    echo 
    "<pre>".print_r($assignmentstrue)."</pre>";
    Last edited by NogDog; 07-07-2012 at 12:43 AM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2006
    Location
    On the web, working on my website
    Posts
    181
    Thank you for the script above - it is working magically. How would I be able to then set a limiter, saying that yes, no more than 7 people in a room, but no less than 5?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,041
    That sounds a bit messier. Maybe after getting to that point, you'd then have to come up with a routine to look for sub-arrays with less than 5 elements, and if you find one, randomly choose one of the sub-arrays with more than 5 elements and "move" one of its members to the underpopulated array?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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



Recent Articles