/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 07.2012 — 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.)
[code=php]
<?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($person, 0, 1)][] = $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($assignments, true)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@youngdesignerauthorJul 07.2012 — 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?
Copy linkTweet thisAlerts:
@NogDogJul 07.2012 — 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?
×

Success!

Help @youngdesigner spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...