Skip to content

Commit

Permalink
Merge pull request #385 from eparovyshnaya/bump-grade-school
Browse files Browse the repository at this point in the history
upgrade `grade-school` exercise implementation to the spec of 1.0.0 version
1. Discrepancies between *description* and *canonical data* are discussed in [#1623 issue for problem-specification](exercism/problem-specifications#1623)
2. As a result:
     - test set and reference implementation are reworked to strictly correspond to the *canonical data*
     - stub implementation is revised accordingly
  • Loading branch information
eparovyshnaya authored Dec 8, 2019
2 parents 992b83a + 06923f2 commit 4061fd2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import java.util.HashMap

class School {

private val database = mutableMapOf<Int, List<String>>()
fun db() = HashMap(database)

fun add(student: String, grade: Int) {
database[grade] = grade(grade) + student
database[grade] = (grade(grade) + student).sorted()
}

fun grade(grade: Int) = database[grade] ?: listOf()

fun sort() = database.toSortedMap().mapValues { it.value.sorted() }
fun roster() = database.toSortedMap().map { it.value }.flatten()

}
1 change: 1 addition & 0 deletions exercises/grade-school/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
5 changes: 1 addition & 4 deletions exercises/grade-school/src/main/kotlin/GradeSchool.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class School {

fun db(): Map<Int, List<String>> =
TODO("Implement this function to complete the task")

fun add(student: String, grade: Int) {
TODO("Implement this function to complete the task")
}
Expand All @@ -11,7 +8,7 @@ class School {
TODO("Implement this function to complete the task")
}

fun sort(): Map<Int, List<String>> {
fun roster(): List<String> {
TODO("Implement this function to complete the task")
}
}
119 changes: 62 additions & 57 deletions exercises/grade-school/src/test/kotlin/SchoolTest.kt
Original file line number Diff line number Diff line change
@@ -1,84 +1,89 @@
import org.junit.Before
import org.junit.Test
import org.junit.Ignore
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SchoolTest {

private lateinit var school: School

@Before
fun beforeTest() {
school = School()
}


@Test
fun startsWithNoStudents() {
assertTrue(school.db().isEmpty())
}
fun `adding a student adds them to the sorted roster`() =
students("Aimee" to 2)
.everyone()
.shouldBe(listOf("Aimee"))

@Ignore
@Test
fun addsStudents() {
school.add("Aimee", 2)

val expected = mapOf(2 to listOf("Aimee"))
assertEquals(expected, school.db())
}
fun `adding more student adds them to the sorted roster`() =
students(
"Blair" to 2,
"James" to 2,
"Paul" to 2
)
.everyone()
.shouldBe(listOf("Blair", "James", "Paul"))

@Ignore
@Test
fun addsMoreStudentsInSameGrade() {
val grade = 2
school.add("James", grade)
school.add("Blair", grade)
school.add("Paul", grade)

val expected = mapOf(2 to listOf("James", "Blair", "Paul"))
assertEquals(expected, school.db())
}
fun `adding students to different grades adds them to the same sorted roster`() =
students(
"Chelsea" to 3,
"Logan" to 7
)
.everyone()
.shouldBe(listOf("Chelsea", "Logan"))

@Ignore
@Test
fun addsStudentsInMultipleGrades() {
school.add("Chelsea", 3)
school.add("Logan", 7)

val expected = mapOf(3 to listOf("Chelsea"), 7 to listOf("Logan"))
assertEquals(expected, school.db())
}
fun `roster returns an empty list if there are no students enrolled`() =
students()
.everyone()
.shouldBe(listOf())

@Ignore
@Test
fun getsStudentsInAGrade() {
school.add("Franklin", 5)
school.add("Bradley", 5)
school.add("Jeff", 1)

val expected = mapOf(5 to listOf("Franklin", "Bradley"), 1 to listOf("Jeff"))
assertEquals(expected, school.db())
}
fun `student names with grades are displayed in the same sorted roster`() =
students(
"Peter" to 2,
"Anna" to 1,
"Barb" to 1,
"Zoe" to 2,
"Alex" to 2,
"Jim" to 3,
"Charlie" to 1
)
.everyone()
.shouldBe(listOf("Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"))

@Ignore
@Test
fun getsStudentsInEmptyGrade() {
assertTrue(school.grade(1).isEmpty())
}
fun `grade returns the students in that grade in alphabetical order`() =
students(
"Franklin" to 5,
"Bradley" to 5,
"Jeff" to 1
)
.fromGrade(5)
.shouldBe(listOf("Bradley", "Franklin"))

@Ignore
@Test
fun sortsSchool() {
school.add("Jennifer", 4)
school.add("Kareem", 6)
school.add("Christopher", 4)
school.add("Kyle", 3)
fun `grade returns an empty list if there are no students in that grade`() =
students()
.fromGrade(1)
.shouldBe(listOf())
}

val expected = mapOf(3 to listOf("Kyle"), 4 to listOf("Christopher", "Jennifer"), 6 to listOf("Kareem"))
val sortedClasses = school.sort()
assertEquals(listOf(3, 4, 6), sortedClasses.keys.toList(), "Grades not sorted in ascending order")
assertEquals(expected, sortedClasses)
private class Students(vararg students: Pair<String, Int>) {
private val school = School()

init {
students.forEach { school.add(it.first, it.second) }
}

fun fromGrade(grade: Int) = StudentNames(school.grade(grade))
fun everyone() = StudentNames(school.roster())
}

private class StudentNames(private val names: List<String>) {
fun shouldBe(names: List<String>) = assertEquals(names, this.names)
}

private fun students(vararg students: Pair<String, Int>) = Students(*students)

0 comments on commit 4061fd2

Please sign in to comment.