There's more than one way to do things PerlMonks

comment on

 Need Help??
```package Schedule::RoundRobin;
######################################################################
+#########
# implements a round robin scheduling algorithm as described in
# http://forum.swarthmore.edu/dr.math/problems/kinley.3.31.00.html
######################################################################
+#########

# takes an array and returns a reference to a list of pairs
sub schedule
{
my(@teams) = @_;

# get the size of the array
my \$size = scalar @teams;

# If it is even, pop off one item and maintain it as a central poi
+nt
# and proceed as with an odd number
return undef unless (\$size > 0);

my \$pivot = undef;
my \$even = 0;
my \$end_point_a = 0;
my \$end_point_b = \$size-1;
if(\$size%2 == 0)
{
# \$pivot = pop @teams;
\$pivot = \$#teams;
\$even = 1;
\$end_point_b--;
}

# Create a list of the "stripes" of the polygon, the stripes will
+be
# pairs of indices into the array
my @stripes;

# Assume that each element in the array is a vertex of the polygon
# and the vertices are listed in order, stripes are created

for (1..int((\$size-1)/2))
{
push(@stripes, [\$end_point_a, \$end_point_b]);
\$end_point_a++;
\$end_point_b--;
}

# Upon falling out of the loop, the middle point will be in both
# end_point_a and end_point_b
if(\$even)
{
push(@stripes, [\$end_point_a, \$pivot]);
}

# Each element of the games array is a play date's worth of games
# so it is an array of pairs
my @games;

for(1..(\$size-\$even))
{
my @this_week;
# push the weeks worth of games onto @games
foreach my \$sched_ref (@stripes)
{
push (@this_week, [\$teams[\$sched_ref->[0]], \$teams[\$sched_ref-
+>[1]]]);
}
push(@games, \@this_week);

# Now rotate the @teams array
my \$last_team_save = undef;
# Save the last team as the pivot for the polygon
if(\$even)
{
\$last_team_save = pop(@teams);
}
my \$last_team = pop(@teams);
unshift(@teams, \$last_team);
if(\$even)
{
push(@teams, \$last_team_save);
}
}
return \@games;
}

1;

In reply to Round Robin Scheduling by modred

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2023-03-31 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (75 votes). Check out past polls.

Notices?