Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-13-2008, 09:46 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Programming Exercise #4: Super fun edition

Today's challenge will include 2 problems:
  1. hard Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice.
    The point of this exercise is to GENERATE DISCUSSION. We are not asking you to WRITE the code, merely speculate as to how you might do it. BUT IF YOU FEEL COMPELLED, write the code to accomodate arbitrary lists from the Dean.
  2. easy Solve this problem.

Note From Hell Raven: the 100 students are rooming in pairs of 2. So that is 50 pairs (I do not believe this was clear...)
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/

Last edited by Kristi; 06-16-2008 at 07:29 PM..
Reply With Quote
  #2  
Old 06-14-2008, 12:47 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Only primes are incompatible to other numbers? There's only 61 primes below 400. Isn't this easily solvable by including everyone who isn't a prime until you run out of space?
Reply With Quote
  #3  
Old 06-14-2008, 01:10 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by DrakilorP2P View Post
Only primes are incompatible to other numbers? There's only 61 primes below 400. Isn't this easily solvable by including everyone who isn't a prime until you run out of space?
I guess the problem is figuring out who is a prime and who isn't ... but if I recall that's quite easy.
__________________
Reply With Quote
  #4  
Old 06-14-2008, 03:01 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Quote:
Originally Posted by cbk1994 View Post
I guess the problem is figuring out who is a prime and who isn't ... but if I recall that's quite easy.
It's basic level programming.
Reply With Quote
  #5  
Old 06-14-2008, 03:15 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
It doesn't matter who is included. Make it include only 100 people, I don't care... Finding primes is trivial. I'm sure I could go online and find a list of them, this is not the point of the exercise.

I have done this for you:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #6  
Old 06-14-2008, 04:25 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Tolnaftate2004 View Post
It doesn't matter who is included. Make it include only 100 people, I don't care... Finding primes is trivial. I'm sure I could go online and find a list of them, this is not the point of the exercise.

I have done this for you:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397
Easiest way has GOT to be to loop through every possible combination of two numbers that could possibly be a prime. Most efficient, too!
__________________
Reply With Quote
  #7  
Old 06-14-2008, 04:39 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by cbk1994 View Post
Easiest way has GOT to be to loop through every possible combination of two numbers that could possibly be a prime. Most efficient, too!
I updated the exercise. So, primes are no longer involved.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #8  
Old 06-14-2008, 04:53 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
PHP Code:
function onCreated()
{
  
temp.start = timevar2;
  
  
temp.incompatible = {
                        { 
1, 2 },
                        { 
3, 4 },
                        { 
5, 6 },
                        { 
6, 7 }
                      };
  
  
temp.chosen = null;
  
temp.sets = null;
  
  for ( 
temp.i = 0; i < 100; i ++ )
  {
    
temp.moveOn = false;
    
    while ( ! 
moveOn )
    {
      
temp.person1 = int( random( 0, 400 ) );
      
temp.person2 = int( random( 0, 400 ) );
      
      if ( 
chosen.index( @ person1 ) != -1 || chosen.index( @ person2 ) != -1 || person1 == person2 )
      {
        continue;
      }
      
      for ( 
temp.a : incompatible )
      {
        if ( ( 
a[0] == person1 && a[1] == person2 ) || ( a[0] == person2 || a[1] == person1 ) )
        {
          
// Incompatible
          
continue;
        }
      }
      
      
chosen.add( person1 );
      
chosen.add( person2 );
      
      
sets.add( { person1, person2 } );
      
      
moveOn = true;
    }
  }
  
  for ( 
temp.set : sets )
  {
    echo( 
"Set:" SPC set[0] SPC "and" SPC set[1] );
  }
  
  echo( 
"Time taken:" SPC timevar2 - start SPC "seconds." );
} 
I made it as inefficient as possible. It's rather fun to make inefficient code .
I don't know why.

PHP Code:
Set: 59 and 119
Set
: 166 and 136
Set
: 279 and 156
Set
: 131 and 142
Set
: 195 and 397
Set
: 364 and 340
Set
: 314 and 382
Set
: 326 and 216
Set
: 352 and 140
Set
: 366 and 139
Set
: 12 and 32
Set
: 104 and 21
Set
: 92 and 75
Set
: 307 and 68
Set
: 271 and 367
Set
: 187 and 38
Set
: 103 and 66
Set
: 194 and 235
Set
: 209 and 390
Set
: 232 and 174
Set
: 331 and 147
Set
: 363 and 109
Set
: 398 and 329
Set
: 249 and 11
Set
: 362 and 354
Set
: 29 and 312
Set
: 170 and 337
Set
: 380 and 42
Set
: 304 and 167
Set
: 80 and 8
Set
: 234 and 275
Set
: 244 and 44
Set
: 265 and 76
Set
: 218 and 197
Set
: 224 and 375
Set
: 55 and 188
Set
: 84 and 54
Set
: 118 and 334
Set
: 288 and 98
Set
: 135 and 318
Set
: 10 and 305
Set
: 347 and 160
Set
: 158 and 28
Set
: 169 and 393
Set
: 303 and 13
Set
: 90 and 255
Set
: 230 and 22
Set
: 302 and 138
Set
: 240 and 37
Set
: 57 and 251
Set
: 343 and 313
Set
: 242 and 291
Set
: 73 and 0
Set
: 319 and 243
Set
: 394 and 223
Set
: 256 and 31
Set
: 392 and 346
Set
: 287 and 359
Set
: 261 and 117
Set
: 33 and 58
Set
: 186 and 283
Set
: 201 and 88
Set
: 126 and 78
Set
: 293 and 69
Set
: 360 and 65
Set
: 309 and 130
Set
: 162 and 95
Set
: 112 and 49
Set
: 339 and 200
Set
: 214 and 106
Set
: 184 and 231
Set
: 47 and 105
Set
: 15 and 237
Set
: 133 and 282
Set
: 205 and 72
Set
: 207 and 61
Set
: 146 and 125
Set
: 381 and 257
Set
: 64 and 149
Set
: 128 and 268
Set
: 324 and 322
Set
: 2 and 122
Set
: 226 and 46
Set
: 306 and 51
Set
: 178 and 35
Set
: 248 and 185
Set
: 369 and 155
Set
: 43 and 145
Set
: 183 and 311
Set
: 91 and 353
Set
: 14 and 115
Set
: 157 and 358
Set
: 213 and 171
Set
: 289 and 350
Set
: 290 and 286
Set
: 87 and 388
Set
: 217 and 159
Set
: 294 and 357
Set
: 148 and 24
Set
: 374 and 1
Time taken
: 0.008700847 seconds. 
__________________
Reply With Quote
  #9  
Old 06-14-2008, 05:11 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
I tried your code and it failed to perform the task. Leaving this to chance is probably a bad idea, seeing as it could lead to an infinite loop.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #10  
Old 06-14-2008, 05:15 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Tolnaftate2004 View Post
I tried your code and it failed to perform the task. Leaving this to chance is probably a bad idea, seeing as it could lead to an infinite loop.
Worked fine for me ;o

The whole goal was inefficiency
__________________
Reply With Quote
  #11  
Old 06-14-2008, 05:57 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by cbk1994 View Post
Worked fine for me ;o

The whole goal was inefficiency
Well I changed the incompatibilities so that any person would only match up with one other person.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #12  
Old 06-14-2008, 06:12 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Tolnaftate2004 View Post
Well I changed the incompatibilities so that any person would only match up with one other person.
o

Probably hit a max loop limit.
__________________
Reply With Quote
  #13  
Old 06-14-2008, 07:26 AM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
I suppose what you would have to do is this:

Starting with 400 students, loop through and select 100 students at random.
Then go through those 100 students and group then until you have 50 groups.
If you have 2 students who are together on the incompatibility list, flip them with another set at random, as described below:

1 & 2
v
^
3 & 4

3 & 2
1 & 4

If any of those 2 are on the incompatibility list, then repeat the process. Once everything is OK, then you've got your list.

Here's the script I made to accompany this concept:

PHP Code:
function onCreated()
{
  
this.start = timevar2;
  
  
this.incompatability = {
    {
102, 284},
    {
392, 126},
    {
1, 2},
    {
3, 4},
    {
5, 6},
    {
7, 8}
  };
  
  
  
this.studentsSelected.clear();
  
this.studentGroups.clear();
  
  
numStudentsSelected = 0;
  
// Select students at random
  
for (i = 0; i < 100; i++)
  {
    
f = int(random(0, 400));
    
    if (
this.studentsSelected.index(f) == -1)
    {
      
this.studentsSelected.add(f);
    } else
    {
      
i--; continue;
    }
  }
  
  
// Group them
  
for (i = 0; i < this.studentsSelected.size(); i += 2)
  {
    
this.studentGroups.add({this.studentsSelected[i], this.studentsSelected[i+1]});
  }
  
  
// Remove incompatabilities
  
for (i = 0; i < 50; i++)
  {
    
sGroup = this.studentGroups[i];
    
    for (
a = 0; a < this.incompatability.size(); a++)
    {
      if ((
sGroup[0] == this.incompatability[a][0] || sGroup[0] == this.incompatability[a][1]) && (sGroup[1] == this.incompatability[a][0] || sGroup[1] == this.incompatability[a][1]))
      {
        echo(
"INCOMPATABILITY: " @ sGroup);
        
        
// we've found an incompatability, so lets flip this student with the next group
        
sGroup2 = this.studentGroups[i+1];
        
        
sGroup[0] = sGroup2[0];
      }
    }
  }
  
  
this.end = timevar2;
  
  echo(
this.end - this.start);
  
  for (
i = 0; i < 50; i++)
  {
    echo(
"Group " @ (i+1) @ ": {" @ this.studentGroups[i][0] @ ", " @ this.studentGroups[i][1] @ "}");
  }
} 
Time taken: 0.002021789

Example Output:
PHP Code:
0.002021789
Group 1
: {137, 114}
Group 2: {282, 284}
Group 3: {47, 288}
Group 4: {267, 53}
Group 5: {19, 329}
Group 6: {345, 299}
Group 7: {141, 228}
Group 8: {93, 50}
Group 9: {347, 390}
Group 10: {166, 207}
Group 11: {139, 41}
Group 12: {130, 336}
Group 13: {140, 376}
Group 14: {262, 238}
Group 15: {196, 0}
Group 16: {352, 79}
Group 17: {368, 151}
Group 18: {54, 387}
Group 19: {81, 108}
Group 20: {332, 380}
Group 21: {249, 160}
Group 22: {73, 300}
Group 23: {64, 66}
Group 24: {316, 204}
Group 25: {248, 24}
Group 26: {8, 111}
Group 27: {215, 396}
Group 28: {252, 148}
Group 29: {270, 240}
Group 30: {229, 378}
Group 31: {172, 209}
Group 32: {227, 333}
Group 33: {283, 127}
Group 34: {42, 194}
Group 35: {359, 302}
Group 36: {6, 292}
Group 37: {30, 293}
Group 38: {105, 374}
Group 39: {371, 324}
Group 40: {242, 119}
Group 41: {83, 348}
Group 42: {256, 158}
Group 43: {189, 128}
Group 44: {232, 389}
Group 45: {322, 191}
Group 46: {225, 197}
Group 47: {33, 334}
Group 48: {239, 121}
Group 49: {39, 213}
Group 50: {230, 29} 
Probably not very efficient, but it works. I couldn't find any incompatibilities! XD
__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote
  #14  
Old 06-14-2008, 07:36 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Programmer View Post
Probably not very efficient, but it works. I couldn't find any incompatibilities! XD
This does not test if switching 2 people forms compatibility...
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #15  
Old 06-14-2008, 07:44 AM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by Tolnaftate2004 View Post
This does not test if switching 2 people forms compatibility...
Picky, Picky.

PHP Code:
function onCreated()
{
  
this.start = timevar2;
  
  
this.incompatability = {
    {
102, 284},
    {
392, 126},
    {
1, 2},
    {
3, 4},
    {
5, 6},
    {
7, 8}
  };
  
  
  
this.studentsSelected.clear();
  
this.studentGroups.clear();
  
  
numStudentsSelected = 0;
  
// Select students at random
  
for (i = 0; i < 100; i++)
  {
    
f = int(random(0, 400));
    
    if (
this.studentsSelected.index(f) == -1)
    {
      
this.studentsSelected.add(f);
    } else
    {
      
i--; continue;
    }
  }
  
  
// Group them
  
for (i = 0; i < this.studentsSelected.size(); i += 2)
  {
    
this.studentGroups.add({this.studentsSelected[i], this.studentsSelected[i+1]});
  }
  
  
// Remove incompatabilities
  
for (i = 0; i < 50; i++)
  {
    
sGroup = this.studentGroups[i];
    
    for (
a = 0; a < this.incompatability.size(); a++)
    {
      if ((
sGroup[0] == this.incompatability[a][0] || sGroup[0] == this.incompatability[a][1]) && (sGroup[1] == this.incompatability[a][0] || sGroup[1] == this.incompatability[a][1]))
      {
        echo(
"INCOMPATABILITY: " @ sGroup);
        
        
// we've found an incompatability, so lets flip this student with the next group
        
sGroup2 = this.studentGroups[i+1];
        
        
sGroup[0] = sGroup2[0];
        
        
this.studentGroups[i] = sGroup;
        
this.studentGroups[i+1] = sGroup2;
        
        
i--;
        continue;
      }
    }
  }
  
  
this.end = timevar2;
  
  echo(
this.end - this.start);
  
  for (
i = 0; i < 50; i++)
  {
    echo(
"Group " @ (i+1) @ ": {" @ this.studentGroups[i][0] @ ", " @ this.studentGroups[i][1] @ "}");
  }
} 
Time: 0.001927852
__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote
  #16  
Old 06-14-2008, 08:46 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Programmer View Post
Time: 0.001927852
Aww, mine was 0.008700847 seconds.

Of course, I was trying to make mine inefficient, for some reason ...
__________________
Reply With Quote
  #17  
Old 06-14-2008, 03:14 PM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by cbk1994 View Post
Aww, mine was 0.008700847 seconds.

Of course, I was trying to make mine inefficient, for some reason ...
Perhaps too many nested loops?
__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote
  #18  
Old 06-14-2008, 07:50 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Programmer View Post
Picky, Picky.

PHP Code:
function onCreated()
{
  
this.start = timevar2;
  
  
this.incompatability = {
    {
102, 284},
    {
392, 126},
    {
1, 2},
    {
3, 4},
    {
5, 6},
    {
7, 8}
  };
  
  
  
this.studentsSelected.clear();
  
this.studentGroups.clear();
  
  
numStudentsSelected = 0;
  
// Select students at random
  
for (i = 0; i < 100; i++)
  {
    
f = int(random(0, 400));
    
    if (
this.studentsSelected.index(f) == -1)
    {
      
this.studentsSelected.add(f);
    } else
    {
      
i--; continue;
    }
  }
  
  
// Group them
  
for (i = 0; i < this.studentsSelected.size(); i += 2)
  {
    
this.studentGroups.add({this.studentsSelected[i], this.studentsSelected[i+1]});
  }
  
  
// Remove incompatabilities
  
for (i = 0; i < 50; i++)
  {
    
sGroup = this.studentGroups[i];
    
    for (
a = 0; a < this.incompatability.size(); a++)
    {
      if ((
sGroup[0] == this.incompatability[a][0] || sGroup[0] == this.incompatability[a][1]) && (sGroup[1] == this.incompatability[a][0] || sGroup[1] == this.incompatability[a][1]))
      {
        echo(
"INCOMPATABILITY: " @ sGroup);
        
        
// we've found an incompatability, so lets flip this student with the next group
        
sGroup2 = this.studentGroups[i+1];
        
        
sGroup[0] = sGroup2[0];
        
        
this.studentGroups[i] = sGroup;
        
this.studentGroups[i+1] = sGroup2;
        
        
i--;
        continue;
      }
    }
  }
  
  
this.end = timevar2;
  
  echo(
this.end - this.start);
  
  for (
i = 0; i < 50; i++)
  {
    echo(
"Group " @ (i+1) @ ": {" @ this.studentGroups[i][0] @ ", " @ this.studentGroups[i][1] @ "}");
  }
} 
Time: 0.001927852
Once again, this failed to perform the task.
Again, let's discuss what we ought to be doing to solve this problem. This is a very difficult problem to solve (HRs idea, don't blame me).
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #19  
Old 06-14-2008, 08:03 PM
Dan Dan is offline
Daniel
Join Date: Oct 2007
Posts: 383
Dan is an unknown quantity at this point
Send a message via MSN to Dan
Quote:
Originally Posted by Tolnaftate2004 View Post
Today's challenge will include 2 problems:
  1. hard Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice.
    The point of this exercise is to GENERATE DISCUSSION. We are not asking you to WRITE the code, merely speculate as to how you might do it. BUT IF YOU FEEL COMPELLED, write the code to accomodate arbitrary lists from the Dean.
  2. easy Solve this problem.
Is it possible for you to write in a little more simple English for me? I don't understand the task quite well, though I'd like to give it a go
__________________
Reply With Quote
  #20  
Old 06-14-2008, 08:20 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Dan View Post
Is it possible for you to write in a little more simple English for me? I don't understand the task quite well, though I'd like to give it a go
1. hard You are putting 400 people in houses. Only 100 people will be able to have a house. You have a list of people that can not be in the same house. 2 people must be in each house. Talk about it, don't script it unless you want to.

Is that a bit better?
__________________
Reply With Quote
  #21  
Old 06-15-2008, 04:49 PM
Dan Dan is offline
Daniel
Join Date: Oct 2007
Posts: 383
Dan is an unknown quantity at this point
Send a message via MSN to Dan
Not sure if I entirely understood the task, but I suppose this should work?

PHP Code:
function onCreated() {
  
temp.students_incompatible = {{1,2},{3,49}};
  
temp.students_total = new[400];
  
temp.house_list = {};
  
  while (
temp.house_list.size() < 100) {
    
temp.i = 0;
    while (
temp.students_total[temp.i] == 1)
      
temp.i ++;
    
temp.students_total[temp.i] = 1;
    
temp.j = temp.i;
    
    
temp.go = false;
    while (
temp.students_total[temp.j] == 1 || !temp.go) {
      if ((@ {
temp.i + 1,temp.j + 1}) in temp.students_incompatible) {
        
temp.go = false;
        
temp.j ++;
      }
      else if ((@ {
temp.j + 1,temp.i + 1}) in temp.students_incompatible) {
        
temp.go = false;
        
temp.j ++;
      }
      else if (
temp.students_total[temp.j] == 1)
        
temp.j ++;
      else
        
temp.go = true;
    }
    
temp.students_total[temp.j] = 1;
    
temp.house_list.add({temp.i + 1,temp.j + 1});
  }
  echo(
temp.house_list);
} 
Time: 0.008579015
__________________

Last edited by Dan; 06-15-2008 at 05:07 PM.. Reason: Added time.
Reply With Quote
  #22  
Old 06-15-2008, 05:39 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Uhm, Dan, suppose to be discussion :o
__________________
Reply With Quote
  #23  
Old 06-15-2008, 06:33 PM
Dan Dan is offline
Daniel
Join Date: Oct 2007
Posts: 383
Dan is an unknown quantity at this point
Send a message via MSN to Dan
Quote:
Originally Posted by Chompy View Post
Uhm, Dan, suppose to be discussion :o
Well then this is how I'd make the groups, lol.
__________________
Reply With Quote
  #24  
Old 06-15-2008, 06:35 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Dan View Post
Well then this is how I'd make the groups, lol.
Try explain it with words instead of code
__________________
Reply With Quote
  #25  
Old 06-15-2008, 09:17 PM
Dan Dan is offline
Daniel
Join Date: Oct 2007
Posts: 383
Dan is an unknown quantity at this point
Send a message via MSN to Dan
Quote:
Originally Posted by Chompy View Post
Try explain it with words instead of code
I want to know if it does what it should do tho (if I did what was needed).
__________________
Reply With Quote
  #26  
Old 06-16-2008, 07:36 PM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
You are using very small lists Your algorithms are based on the fact that the list is small. If you took your basic ideas and applied them to more complex or impossible lists, they would run much longer then any of our lifetimes.

By impossible, suppose you were given a list of incompatible students that makes it impossible to come up with 100 students.

Your work is cut out for you
__________________
Reply With Quote
  #27  
Old 06-16-2008, 10:41 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Kristi View Post
If you took your basic ideas and applied them to more complex or impossible lists, they would run much longer then any of our lifetimes.
No, that's why we have a max loop limit
__________________
Reply With Quote
  #28  
Old 06-16-2008, 10:48 PM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by cbk1994 View Post
No, that's why we have a max loop limit
You are supposed to assume a limit doesn't exist (in graal this can be achieved with a scheduleevent).
__________________
Reply With Quote
  #29  
Old 06-16-2008, 11:03 PM
Dan Dan is offline
Daniel
Join Date: Oct 2007
Posts: 383
Dan is an unknown quantity at this point
Send a message via MSN to Dan
Quote:
Originally Posted by Kristi View Post
You are supposed to assume a limit doesn't exist (in graal this can be achieved with a scheduleevent).
OK. Good luck to anyone trying, Dan's not going to invest any more time in this Programming Exercise because there's more work to do .
__________________
Reply With Quote
  #30  
Old 06-18-2008, 10:46 PM
Stryke Stryke is offline
Scripter
Join Date: Apr 2008
Posts: 157
Stryke is an unknown quantity at this point
Would this work?

PHP Code:
function onCreated()
{
  
this.start = timevar2;
  
this.incompatability = {
                         {
23,46},
                         {
29,44},
                         {
45,96},
                         {
11,77},
                         {
2,32},
                         {
1,12},
                         {
13,69},
                         };
  
this.houselist = NULL;
  for(
temp.i=0; temp.i<100; temp.i++)
  {
    
this.intA = 0;
    
this.intB = 0;
    while(!
this.intA && !this.intB)
    {
      
this.intA = int(random(0,400));
      
this.intB = int(random(0,400));
      for(
temp.j=0; temp.j<100; temp.j++)
      {
        if(
this.intA == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intA == this.houselist[temp.j][1])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][1])
          
this.int = 0;
        if(
this.intA == this.incompatibility[temp.j])
          if(
this.intB == this.incompatibility[temp.j])
            
this.int = 0;
        else if(
this.intB == this.incompatibility[temp.j])
          if(
this.intA == this.incompatibility[temp.j])
            
this.int = 0;
      }
      if(
this.intA == this.intB)
        
this.int = 0;
    }
    
this.houselist.add({this.intA,this.intB});
  }
  
this.end = timevar2;
}
savelog2("houseList.txt",this.houselist);
echo(
"Time: " @ this.end - this.start); 
Time: 0.026829004

Damn that's slow compared to others lol
Reply With Quote
  #31  
Old 06-18-2008, 11:00 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Stryke View Post
Would this work?

PHP Code:
function onCreated()
{
  
this.start = timevar2;
  
this.incompatability = {
                         {
23,46},
                         {
29,44},
                         {
45,96},
                         {
11,77},
                         {
2,32},
                         {
1,12},
                         {
13,69},
                         };
  
this.houselist = NULL;
  for(
temp.i=0; temp.i<100; temp.i++)
  {
    
this.intA = 0;
    
this.intB = 0;
    while(!
this.intA && !this.intB)
    {
      
this.intA = int(random(0,400));
      
this.intB = int(random(0,400));
      for(
temp.j=0; temp.j<100; temp.j++)
      {
        if(
this.intA == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intA == this.houselist[temp.j][1])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][0])
          
this.int = 0;
        else if(
this.intB == this.houselist[temp.j][1])
          
this.int = 0;
        if(
this.intA == this.incompatibility[temp.j])
          if(
this.intB == this.incompatibility[temp.j])
            
this.int = 0;
        else if(
this.intB == this.incompatibility[temp.j])
          if(
this.intA == this.incompatibility[temp.j])
            
this.int = 0;
      }
      if(
this.intA == this.intB)
        
this.int = 0;
    }
    
this.houselist.add({this.intA,this.intB});
  }
  
this.end = timevar2;
}
savelog2("houseList.txt",this.houselist);
echo(
"Time: " @ this.end - this.start); 
Time: 0.026829004

Damn that's slow compared to others lol
I didn't try it, but, like said, anything using random will not work well with larger numbers.
__________________
Reply With Quote
  #32  
Old 06-18-2008, 11:00 PM
Dan Dan is offline
Daniel
Join Date: Oct 2007
Posts: 383
Dan is an unknown quantity at this point
Send a message via MSN to Dan
I think it's slow because of the logging you're doing. But yeah, quite some loops also.
__________________
Reply With Quote
  #33  
Old 06-21-2008, 06:36 AM
Stryke Stryke is offline
Scripter
Join Date: Apr 2008
Posts: 157
Stryke is an unknown quantity at this point
Oh it's only 50? :P

Time remaining: 0.007151126

Still crappy. :P I took out the log too.
Reply With Quote
  #34  
Old 06-21-2008, 06:52 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
We can assume that the algorithm would eventually need to be used to find an appropriate room assignment for a group of students for which there is only one solution (i.e. worst case scenario). Because of this, the algorithm would need to "know" that student A needs to pair with student B rather than student C. Hence why random is a horrible idea.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #35  
Old 07-19-2008, 09:26 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
PHP Code:
//#CLIENTSIDE
function onWeaponFired() {
  
// Bad match array
  
temp.bad = {
             {
91,50},
             {
60,230},
             {
132,399},
             {
52,293},
             {
22,25},
             {
83,293},
             {
83,82}
             };
  
// Generate students array
  
temp.students = new[400];
  for (
temp.i = 0; temp.i < 400; temp.i++)
    
temp.students[temp.i] = temp.i;



  
// START of exercise
  
temp.start = timevar2;

  
// Shuffle students array
  
temp.n = temp.students.size();
  for (
temp.i = 0; temp.i < temp.n - 1; temp.i++) {
    
temp.j = temp.i + random(0,32767) / (32767 / (temp.n - temp.i) + 1);
    
temp.t = temp.students[temp.j];
    
temp.students[temp.j] = temp.students[temp.i];
    
temp.students[temp.i] = temp.t;
  }
  
  
// Heapsort the bad match array
  
temp.bad.sortascending();
  
  
temp.pairs = new[50];
  
temp.i = 0;
  while (
temp.i < 50) {
    
temp.pair = {temp.students[temp.x], temp.students[temp.x+1]};

    
// Binary search for the current pair in the bad pairs
    
if (binarysearcharray(temp.bad, temp.pair) == -1) {
      
temp.pairs[temp.i] = temp.pair;
      
temp.x++;
    }
    
temp.x++;
    
temp.i++;
  }
  
  
  
// END of exercise
  
temp.time = timevar2 - temp.start;
    
  
player.chat = temp.pairs;
  echo (
temp.time);
}

// Modified binary search
function binarysearcharray(temp.a, temp.v) {
  
temp.low = 0;
  
temp.high = temp.a.size()-1;
  while (
temp.low <= temp.high) {
    
temp.mid = (temp.low + temp.high) /2;
    if (
temp.a[temp.mid][0] > temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] > temp.v[1]) 
      
temp.high = temp.mid - 1;
    else if (
temp.a[temp.mid][0] < temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] < temp.v[1])
      
temp.low = temp.mid +1;
    else
      return 
temp.mid;
  }
  return -
1;
} 
Average running time of 0.00326736083, but it's a rather small "bad pair" set.

Should run in O(n log n log m) where n is the number of pairs and m is the number of bad pairs.

Last edited by WhiteDragon; 07-20-2008 at 09:20 PM.. Reason: Slight mistake
Reply With Quote
  #36  
Old 07-21-2008, 07:47 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
I realized I made an error, but it wouldn't let me edit it... so here is another giant block of code:

PHP Code:
//#CLIENTSIDE
function onWeaponFired() {
  
// Bad match array
  
temp.bad = {
             {
91,50},
             {
60,230},
             {
132,399},
             {
52,293},
             {
22,25},
             {
83,293},
             {
83,82}
             };
  
// Generate students array
  
temp.students = new[400];
  for (
temp.i = 0; temp.i < 400; temp.i++)
    
temp.students[temp.i] = temp.i;



  
// START of exercise
  
temp.start = timevar2;

  
// Shuffle students array
  
temp.n = temp.students.size();
  for (
temp.i = 0; temp.i < temp.n - 1; temp.i++) {
    
temp.j = temp.i + random(0,32767) / (32767 / (temp.n - temp.i) + 1);
    
temp.t = temp.students[temp.j];
    
temp.students[temp.j] = temp.students[temp.i];
    
temp.students[temp.i] = temp.t;
  }
  
  
// Heapsort the bad match array
  
temp.bad.sortascending();
  
  
temp.pairs = new[50];
  
temp.i = 0;
  while (
temp.i < 50) {
    
temp.pair = {temp.students[temp.x], temp.students[temp.x+1]};

    
// Binary search for the current pair in the bad pairs
    
if (binarysearcharray(temp.bad, temp.pair) == -1) {
      
// Add it
      
temp.pairs[temp.i] = temp.pair;
      
temp.students.remove(temp.students[temp.x]);
      
temp.students.remove(temp.students[temp.y]);
      
temp.i++;
      
temp.x = 0;
      
temp.y = 0;
    } else {
      
// Look more
      
if (temp.y == temp.students.size()) {
        
temp.x++;
        
temp.y = 0;
      } else {
        
temp.y++;
      }
    }
  }
  
  
  
// END of exercise
  
temp.time = timevar2 - temp.start;
    
  
player.chat = temp.pairs;
  echo (
temp.time);
}

// Modified binary search
function binarysearcharray(temp.a, temp.v) {
  
temp.low = 0;
  
temp.high = temp.a.size()-1;
  while (
temp.low <= temp.high) {
    
temp.mid = (temp.low + temp.high) /2;
    if (
temp.a[temp.mid][0] > temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] > temp.v[1]) 
      
temp.high = temp.mid - 1;
    else if (
temp.a[temp.mid][0] < temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] < temp.v[1])
      
temp.low = temp.mid +1;
    else
      return 
temp.mid;
  }
  return -
1;
} 
Worst case is actually O(n**2 log n log m), but it uses Randomization and Restriction, so it's considerably fast than the worst case.
Reply With Quote
  #37  
Old 08-02-2008, 11:24 AM
_Z3phyr_ _Z3phyr_ is offline
Banned
Join Date: Sep 2003
Location: Louisiane
Posts: 390
_Z3phyr_ is an unknown quantity at this point
Quote:
Originally Posted by Tolnaftate2004 View Post
Today's challenge will include 2 problems:
  1. hard Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice.
    The point of this exercise is to GENERATE DISCUSSION. We are not asking you to WRITE the code, merely speculate as to how you might do it. BUT IF YOU FEEL COMPELLED, write the code to accomodate arbitrary lists from the Dean.

Note From Hell Raven: the 100 students are rooming in pairs of 2. So that is 50 pairs (I do not believe this was clear...)

answer:





proof:

N and B are disjoint sets. (see above picture)

---

this is an abstract problem. idk what you guys have been doing with all of that prime number crap.

Last edited by _Z3phyr_; 08-02-2008 at 01:40 PM..
Reply With Quote
  #38  
Old 08-02-2008, 10:35 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
This problem is NP-complete, so it's impossible to do it in polynomial time.
The runtime is actually O(n**m) if you check all conditions. The best way to attack the problem would most likely be dynamic programming.
Reply With Quote
Reply

Tags
programming-exercise


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:31 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.