Given students' names along with the grade that they are in, create a roster for the school.
In the end, you should be able to:
Note that all our students only have one name. (It's a small town, what do you want?)
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?
To run the tests, run the command dotnet test
from within the exercise directory.
Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the Skip
property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using exercism submit GradeSchool.cs
For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.
A pairing session with Phil Battos at gSchool http://gschool.it
// This file was auto-generated based on version 1.0.0 of the canonical data.
using System;
using Xunit;
public class GradeSchoolTest
{
[Fact]
public void Adding_a_student_adds_them_to_the_sorted_roster()
{
var sut = new GradeSchool();
sut.Add("Aimee", 2);
var expected = new[] { "Aimee" };
Assert.Equal(expected, sut.Roster());
}
[Fact(Skip = "Remove to run test")]
public void Adding_more_student_adds_them_to_the_sorted_roster()
{
var sut = new GradeSchool();
sut.Add("Blair", 2);
sut.Add("James", 2);
sut.Add("Paul", 2);
var expected = new[] { "Blair", "James", "Paul" };
Assert.Equal(expected, sut.Roster());
}
[Fact(Skip = "Remove to run test")]
public void Adding_students_to_different_grades_adds_them_to_the_same_sorted_roster()
{
var sut = new GradeSchool();
sut.Add("Chelsea", 3);
sut.Add("Logan", 7);
var expected = new[] { "Chelsea", "Logan" };
Assert.Equal(expected, sut.Roster());
}
[Fact(Skip = "Remove to run test")]
public void Roster_returns_an_empty_list_if_there_are_no_students_enrolled()
{
var sut = new GradeSchool();
var expected = Array.Empty<string>();
Assert.Empty(sut.Roster());
}
[Fact(Skip = "Remove to run test")]
public void Student_names_with_grades_are_displayed_in_the_same_sorted_roster()
{
var sut = new GradeSchool();
sut.Add("Peter", 2);
sut.Add("Anna", 1);
sut.Add("Barb", 1);
sut.Add("Zoe", 2);
sut.Add("Alex", 2);
sut.Add("Jim", 3);
sut.Add("Charlie", 1);
var expected = new[] { "Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim" };
Assert.Equal(expected, sut.Roster());
}
[Fact(Skip = "Remove to run test")]
public void Grade_returns_the_students_in_that_grade_in_alphabetical_order()
{
var sut = new GradeSchool();
sut.Add("Franklin", 5);
sut.Add("Bradley", 5);
sut.Add("Jeff", 1);
var expected = new[] { "Bradley", "Franklin" };
Assert.Equal(expected, sut.Grade(5));
}
[Fact(Skip = "Remove to run test")]
public void Grade_returns_an_empty_list_if_there_are_no_students_in_that_grade()
{
var sut = new GradeSchool();
var expected = Array.Empty<string>();
Assert.Empty(sut.Grade(1));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
public class Student
{
public Student(string name, int grade)
{
Name = name;
Grade = grade;
}
public string Name { get; set; }
public int Grade { get; set; }
}
public class GradeSchool
{
private readonly List<Student> students = new List<Student>();
public void Add(string student, int grade)
{
students.Add(new Student(student, grade));
}
public IEnumerable<string> Roster()
{
return students
.OrderBy(x => x.Grade)
.ThenBy(x => x.Name)
.Select(x => x.Name);
}
public IEnumerable<string> Grade(int grade)
{
return students
.Where(x => x.Grade == grade)
.OrderBy(x => x.Name)
.Select(x => x.Name);
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments