How would I create a Program that would take a list of names, and sort them into EVEN groups?
I want to be able to create a program that will take a list of names from a .txt file (Doesnt have to be a txt, maybe excell, whatever is easiest) and generate those names into groups or "teams".
I have 30 "users" that have to be sorted into "Teams". I want to be able 开发者_如何学Goto do this fairly, so I want to random :P.
I haven't yet decided the amount of persons per team, but I will soon.
3 Teams of 10 5 teams of 6
Thanks for the responce.
If there's only 30 people in the list or so, you can just read the whole text file into memory split by line breaks, randomize it, then go through the list and create groups.
So, something like this:
public List<String[]> CreateTeams(String filePath, int membersPerTeam)
{
String[] allUsers = File.ReadAllLines(filePath);
List<String> randomUsers = allUsers.OrderBy(s => new Guid()).ToList();
int teamCount = allUsers.Length / membersPerTeam;
var teams = new List<String[]>();
for (int i = 0; i < teamCount; i++)
{
String[] team = new String[membersPerTeam];
for (int j = 0; j < usersPerTeam; j++)
{
team[j] = randomUsers[i * membersPerTeam + j];
}
teams.Add(team);
}
return teams;
}
Though obviously you'd need more error checking, etc. and that nested loop is kind of ugly but you get the idea. And of course this won't work if the total number of users is not evenly divisible by the members per team. However, this should get you going.
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
- Load your list of names from the file.
- Create a list of groups/teams.
- While there are still items in the names list, use a random number from 0 to the length of the list to take one item from the list and add to a group (i.e. a contrived shuffle).
You need to research the following to accomplish this:
- Arrays and Collections.
- File access.
- Random numbers.
You haven't provided enough details on where you are having trouble, but here is an overview of what you need to do.
Step 1: Parse the text file. Something like System.IO.File.ReadAllLines
is probably fine for this.
Step 2: Shuffle the list randomly. This is pretty easy. Try something like, this.
Step 3: Partition the list into consecutive groups. So for 3 teams of 10, just take the first, second, and third set of 10 names and those are your teams.
If this is a one time thing, I'd just do it myself by pasting the text file into http://www.random.org/lists/ and then doing step 3 by hand.
精彩评论