Shannon's Seating Chart

// Copyright 2010 by Grant Jenks - All rights reserved.

using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
  const int GroupCount = 8;
  static string[] GroupNames = { "Alkali Metals",
				 "Alkaline Earth Metals",
				 "Transition Metals",
				 "Noble Gases",
				 "Halogens",
				 "Popular Nonmetals",
				 "Lanthanides",
				 "Actinides" };
  static string[,] Seats = {
    { "Li", "Rb", "Na", "K" },
    { "Mg", "Ca", "Ba", "Be" },
    { "Fe", "Co", "Au", "Cu" },
    { "He", "Ne", "Xe", "Ar" },
    { "Br", "F", "I", "Cl" },
    { "H", "N", "C", "O" },
    { "Yb", "Nd", "Pm", "Er" },
    { "Cf", "Es", "Pu", "U" } };

  static string GetInput()
  {
    string fileName = Path.GetTempFileName();

    Process notepad = Process.Start(@"C:\Windows\System32\notepad.exe",
				    fileName);
    notepad.WaitForExit();

    String text;

    using (StreamReader sr = new StreamReader(fileName))
    {
      text = sr.ReadToEnd();
    }

    File.Delete(fileName);

    return text;
  }

  static List<string> GetStudents(string text)
  {
    List<string> studentNames = new List<string>();
    string pattern = @"([\S ]+), ([\S ]+).*\r\n";
    foreach (Match match in Regex.Matches(text, pattern))
      {
	string name = String.Format("{0} {1}",
				    match.Groups[2].Value.Trim(),
				    match.Groups[1].Value.Trim());
	studentNames.Add(name);
      }
    return studentNames;
  }

  public static List<T> ShuffleList<T>(List<T> list)
  {
    T[] shuffle = list.ToArray();
    int length = shuffle.Length;
    int swaps = length * length * length * length;
    Random random = new Random(DateTime.Now.Millisecond);
    for (int i = 0; i < swaps; i++)
      {
	int left = random.Next(length);
	int right = random.Next(length);
	T temp = shuffle[left];
	shuffle[left] = shuffle[right];
	shuffle[right] = temp;
      }
    return ((IEnumerable<T>)shuffle).ToList();
  }

  static List<List<string>> FormGroups(List<string> students)
  {
    int number = GroupCount;

    students = ShuffleList<string>(students);

    List<List<string>> groups = new List<List<string>>();
    for (int i = 0; i < number; i++)
      groups.Add(new List<string>());

    int count = students.Count;
    for (int i = 0; i < count; i++)
      {
	groups[i%number].Add(students[i]);
      }

    groups = ShuffleList<List<string>>(groups);
    return groups;
  }

  static void PrintGroups(List<List<string>> groups)
  {
    string fileName = Path.GetTempFileName();

    using (StreamWriter sw = new StreamWriter(fileName))
    {
      int groupNumber = 0;
      foreach (List<string> group in groups)
	{
	  sw.WriteLine("Group: {0}", GroupNames[groupNumber]);
	  
	  List<string> seats = new List<string>();
	  for (int i = 0; i < 4; i++)
	    seats.Add(Seats[groupNumber, i]);
	  seats = ShuffleList<string>(seats);
	  
	  int seatNumber = 0;
	  foreach (string student in group)
	    {
	      sw.WriteLine("{0}: {1}", seats[seatNumber], student);
	      seatNumber++;
	    }
	  groupNumber++;
	}
    }

    Process notepad = Process.Start(@"C:\Windows\System32\notepad.exe",
				    fileName);
    notepad.WaitForExit();

    File.Delete(fileName);
  }

  static int Main(string[] args)
  {
    string text = GetInput();

    List<string> students = GetStudents(text);

    List<List<string>> groups = FormGroups(students);

    PrintGroups(groups);

    return 0;
  }
}
projects/shannon_s_seating_chart.txt · Last modified: 2012/04/30 23:41 by grant