Since this isn't homework and it looked like a fun little project... (This is one huge spoiler)
Console app built for .Net 4.0 with VS 2010 Beta 2
class Program
{
static void Main()
{
/* Assuming one team per line */
Team[] teams = File.ReadAllLines("teams.txt")
.Select(t => new Team(t))
.ToArray();
/* Guid.NewGuid() is creates a sufficiently random order */
/* Assuming one player per line */
string[] players = File.ReadAllLines("players.txt")
.OrderBy(s => Guid.NewGuid())
.ToArray();
/*
* No use randomizing anymore...
* Just assign (PlayerCount / TeamCount) players to each team
*/
for (int i = 0; i < teams.Length; i++)
{
var p = players.Skip(i * players.Length / teams.Length)
.Take(players.Length / teams.Length);
teams[i % teams.Length].Players.AddRange(p);
}
foreach (Team t in teams)
{
System.Diagnostics.Debug.WriteLine(t);
}
}
}
class Team
{
public string Name { get; set; }
public List<string> Players { get; set; }
public Team(string name)
{
Name = name;
Players = new List<string>();
}
public override string ToString()
{
/* Team name plus the players sorted alphabetically */
/*//.Net 4.0
return string.Format("{0} \n{1}",
Name,
string.Join(" \n",
Players.Select(p => string.Concat("\t", p))
.OrderBy(s => s)));
*/
//.Net 3.5 & 4.0
return string.Format("{0} \n{1}",
Name,
string.Join(" \n",
Players.Select(p => string.Concat("\t", p))
.OrderBy(s => s).ToArray()));
}
}
Example output:
Colts
Chuck
Cory
Jim
Sam
Ulysses
Saints
Al
Brett
Hank
Ned
Quinn
Vikings
Dave
Don
Ernie
Frank
Gus
Jets
Bob
Eric
Isaac
Walt
Yancy
Chargers
Abe
Mark
Oscar
Xavier
Zach
Cardinals
Fred
Kyle
Pete
Ralph
Tom