exercism/csharp/grade-school/GradeSchool.cs

27 lines
705 B
C#
Raw Permalink Normal View History

2018-03-02 22:27:55 +00:00
using System.Collections.Generic;
2018-03-12 19:21:16 +00:00
using System.Linq;
2018-03-02 22:27:55 +00:00
public class School
{
2018-03-12 19:21:16 +00:00
private Dictionary<int, List<string>> roster = new Dictionary<int, List<string>>();
2018-03-02 22:27:55 +00:00
public void Add(string student, int grade)
{
2018-03-12 19:21:16 +00:00
if (roster.ContainsKey(grade))
roster[grade].Add(student);
else
roster.Add(grade, new List<string> {student});
2018-03-02 22:27:55 +00:00
}
public IEnumerable<string> Roster()
2018-03-12 19:21:16 +00:00
=> roster.Keys
.OrderBy(g => g)
.SelectMany(g => Grade(g))
.ToList();
2018-03-02 22:27:55 +00:00
public IEnumerable<string> Grade(int grade)
2018-03-12 19:21:16 +00:00
=> roster.ContainsKey(grade)
? roster[grade].OrderBy(g => g).ToList()
: new List<string>();
}