using System.Collections.Generic;
using System.Text.RegularExpressions;
static int ComputeDecrementLoops(int startValue)
static IList<string> GetStringsOfLength(IEnumerable<string> strings, int minLength, int maxLength)
return strings.Where(s => s.Length >= minLength && s.Length < maxLength)
static void TrackInvocations(EventReporter obj)
obj.Invoked += delegate(object sender, EventArgs args) { EventReporter.InvocationCount++; };
static ILookup<string, Person> FindByBirthdayMonth(IEnumerable<Person> people, int month)
return people.Where(p => p.Birthday.Month == month).ToLookup(p => p.Department);
static bool IsValidCodeFormat(string code)
return code != null && Regex.IsMatch(code, @"^#[A-Z]{3}\d{3,5}(\{[A-Z]\})?[+|-]");
class Planet : IEquatable<Planet>, IComparable<Planet>
public string Designator { get; set; }
public float Mass { get; set; }
public override string ToString()
public override int GetHashCode()
return Designator.GetHashCode();
public bool Equals(Planet other)
return other != null && String.Equals(this.Designator, other.Designator);
public int CompareTo(Planet other)
return other == null ? 1 :
(Mass < other.Mass ? -1 : Math.Abs(Mass - other.Mass) < float.Epsilon ? 0 : 1);
class SomeBigResource : IDisposable
public static string SomeCode = null;
private bool _disposed = false;
SomeCode = new Guid().ToString();
public string GetDataFromUnmanagedResouce()
if (_disposed) throw new ObjectDisposedException("SomeCode");
GC.SuppressFinalize(this);
public static void Main(string[] args)
var strings = GetStringsOfLength("yellow|blue|green|red|black|white|pink|brown|cyan".Split('|'), 4, 5);
Assert(strings.Intersect("blue|cyan|pink".Split('|')).Count() == 3);
Assert(strings[0] == "blue");
new Person{Id=1, GivenName = "Bill", FamilyName = "Smith", Department = "Sales", Birthday = DateTime.Parse("7/1/1980")},
new Person{Id=2, GivenName = "Alice", FamilyName = "Jones", Department = "Support", Birthday = DateTime.Parse("10/15/2011")},
new Person{Id=3, GivenName = "Jenny", FamilyName = "James", Department = "Sales", Birthday = DateTime.Parse("7/15/1950")},
new Person{Id=4, GivenName = "Kevin", FamilyName = "Scott", Department = "Support", Birthday = DateTime.Parse("7/10/1979")},
new Person{Id=5, GivenName = "Jerry", FamilyName = "Richards", Department = "Support", Birthday = DateTime.Parse("3/6/1972")}
var lu = FindByBirthdayMonth(people, 7);
Assert(lu["Sales"].Any(p => p.Id == 1));
Assert(lu["Sales"].Any(p => p.Id == 3));
Assert(lu["Support"].Any(p => p.Id == 4));
Assert(ComputeDecrementLoops(3000) == 12);
Assert(!IsValidCodeFormat(""));
Assert(!IsValidCodeFormat(null));
Assert(!IsValidCodeFormat(" #ATD18829+"));
Assert(!IsValidCodeFormat("#AT1982-"));
Assert(!IsValidCodeFormat("ATF88198+"));
Assert(!IsValidCodeFormat("ATF+"));
Assert(!IsValidCodeFormat("#AKD901."));
Assert(!IsValidCodeFormat("#AKD901{.}"));
Assert(IsValidCodeFormat("#ABC123+ "));
Assert(IsValidCodeFormat("#ABC123+"));
Assert(IsValidCodeFormat("#ABC1234+"));
Assert(IsValidCodeFormat("#ABC12345+"));
Assert(IsValidCodeFormat("#ABC12345-"));
Assert(IsValidCodeFormat("#ABC12345{A}+"));
new Planet {Designator = "P105", Mass = 1000f},
new Planet {Designator = "ZX6005", Mass = 2000f},
new Planet {Designator = "P1009", Mass = 2000f},
new Planet {Designator = "YX99180", Mass = 5000f},
Assert(planets[0].ToString() == "P105");
Assert(planets[0].GetHashCode() == "P105".GetHashCode());
Assert(!planets[0].Equals(planets[1]));
Assert(planets[1].CompareTo(planets[2]) == 0);
Assert(planets[3].CompareTo(planets[2]) == 1);
using (var br = new SomeBigResource())
Assert(br.GetDataFromUnmanagedResouce() == SomeBigResource.SomeCode);
Assert(SomeBigResource.SomeCode == null);
var br2 = new SomeBigResource();
Assert(br2.GetDataFromUnmanagedResouce() == SomeBigResource.SomeCode);
{ br2.GetDataFromUnmanagedResouce(); }
catch (ObjectDisposedException)
Assert(SomeBigResource.SomeCode == null);
var er = new EventReporter();
for (var c = 0; c < 15; c++) er.Invoke();
Assert(EventReporter.InvocationCount == 15);
Console.WriteLine("All tests passed!");
static void SomeOtherFn()
var br = new SomeBigResource();
static void Assert(bool condition)
if (!condition) throw new Exception("Test failed.");
public int Id { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public DateTime Birthday { get; set; }
public string Department { get; set; }
public static int InvocationCount { get; set; }
public event EventHandler<EventArgs> Invoked;
public void Invoke() { if (Invoked != null) Invoked(this, EventArgs.Empty); }