June 10, 2010

Sweepstake Generator

Highly topical now the world cup is on! Here is my sweepstake generator class
class SweepstakeGenerator
{
  public class PlayerTeamPair
  {
    public string Player { get; set; }
    public string Team { get; set; }
    public override string ToString()
    {
      return (string.IsNullOrEmpty(Team) || string.IsNullOrEmpty(Player)) ? 
        "" : Team + " - " + Player;
    }
  }

  public static IEnumerable AssignTeams(
    IEnumerable orderedTeams, 
    IEnumerable namesArray)
  {
    Debug.Assert(namesArray.Count() > 0, "No names in the names array!");
    List names = new List();
    List teams = new List(orderedTeams);
    List assignedTeams = new List(teams.Count);
    Random random = new Random();
    int teamIx = 0;
    while (teamIx < teams.Count)
    {
      if (names.Count == 0)
      {
        names = names = new List(namesArray);
      }
      int nameIx = random.Next(0, names.Count);
      assignedTeams.Add(new PlayerTeamPair() { 
        Team = teams[teamIx], 
        Player = names[nameIx] });
      //Debug.WriteLine(teams[teamIx] + " - " + names[nameIx]);
      //Console.WriteLine(teams[teamIx] + " - " + names[nameIx]);
      names.RemoveAt(nameIx);
      teamIx++;
    }
    return assignedTeams;
  }
}
and sample usage
string[] teamsOrderedByBettingOdds = new string[] { 
 "Spain", "Brazil", "Argentina", "England",
 "Holland", "Germany", "Italy", "France",
 "Portugal", "Ivory Coast", "Serbia", "Mexico",
 "Chile", "USA", "Paraguay", "Uruguay",
 "Cameroon", "South Africa", "Ghana", "Australia",
 "Denmark", "Nigeria", "Greece", "South Korea",
 "Switzerland", "Slovakia", "Slovenia", "Japan",
 "Algeria", "Honduras", "North Korea", "New Zealand"
 };
string[] teamsOrderedByGrouping = new string[] { 
 "France", "Mexico", "South Africa", "Uruguay",
 "Argentina", "Greece", "Nigeria", "South Korea",
 "Algeria", "England", "Slovenia", "USA",
 "Australia", "Germany", "Ghana", "Serbia",
 "Cameroon", "Denmark", "Japan", "Netherlands",
 "Italy", "New Zealand", "Paraguay", "Slovakia",
 "Brazil", "Ivory Coast", "North Korea", "Portugal",
 "Chile", "Honduras", "Spain", "Switzerland"
 };
string[] teamsOrderedTop8AndThenByGrouping = new string[] { 
 "France", "Argentina", "England", "Germany",
 "Holland", "Italy", "Brazil", "Spain",
 "Mexico", "South Africa", "Uruguay", "Greece",
 "Nigeria", "South Korea", "Algeria", "Slovenia",
 "USA", "Australia", "Ghana", "Serbia",
 "Cameroon", "Denmark", "Japan", "New Zealand",
 "Paraguay", "Slovakia", "Ivory Coast", "North Korea",
 "Portugal", "Chile", "Honduras", "Switzerland" };
 
string[] namesArray = new string[] { 
  "Bob", "Ron", "Tony", "Jim", "George", "Ben", "Alf", "Dirk" };

IEnumerable playerTeamIter = 
  SweepstakeGenerator.AssignTeams(
    teamsOrderedTop8AndThenByGrouping, namesArray);
foreach (SweepstakeGenerator.PlayerTeamPair playerTeam in playerTeamIter)
{
 Debug.WriteLine(playerTeam.ToString());
}
Sample Output
France - George
Argentina - Tony
England - Alf
Germany - Ron
Holland - Bob
Italy - Jim
Brazil - Ben
Spain - Dirk
Mexico - Jim
South Africa - Ben
Uruguay - George
Greece - Dirk
Nigeria - Alf
South Korea - Ron
Algeria - Bob
Slovenia - Tony
USA - Dirk
Australia - Jim
Ghana - Alf
Serbia - George
Cameroon - Tony
Denmark - Ben
Japan - Ron
New Zealand - Bob
Paraguay - Dirk
Slovakia - Alf
Ivory Coast - Jim
North Korea - Ben
Portugal - Ron
Chile - Bob
Honduras - George
Switzerland - Tony

No comments: