-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from eparovyshnaya/bump-grade-school
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
Showing
4 changed files
with
66 additions
and
66 deletions.
There are no files selected for viewing
7 changes: 2 additions & 5 deletions
7
exercises/grade-school/.meta/src/reference/kotlin/GradeSchool.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |