|
| 1 | +package java_problem.company_test |
| 2 | + |
| 3 | +typealias Id = Int |
| 4 | + |
| 5 | +data class Course(val id: Id, val name: String, val isPaid: Boolean) |
| 6 | +data class Student(val id: Id, val name: String, val subscribedCourses: List<Course>) |
| 7 | +interface Repository<T> { |
| 8 | + fun get(): Iterable<T> |
| 9 | +} |
| 10 | + |
| 11 | +class RepoImpl<T> : Repository<Student> { |
| 12 | + override fun get(): Iterable<Student> { |
| 13 | + return listOf( |
| 14 | + Student( |
| 15 | + id = 1, |
| 16 | + name = "MK", |
| 17 | + subscribedCourses = listOf( |
| 18 | + Course(id = 1, name = "Maths", isPaid = true), |
| 19 | + Course(id = 2, name = "Arts", isPaid = true) |
| 20 | + ) |
| 21 | + ), |
| 22 | + Student( |
| 23 | + id = 1, |
| 24 | + name = "DK", |
| 25 | + subscribedCourses = listOf( |
| 26 | + Course(id = 1, name = "Maths", isPaid = false), |
| 27 | + Course(id = 2, name = "Arts", isPaid = true) |
| 28 | + ) |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +class University(private val repository: Repository<Student>) { |
| 38 | + fun getPaidCoursesWithTheNumbersOfSubscribedStudents(coursesCount: Int): Map<Course, Int> { |
| 39 | + val students = repository.get(); |
| 40 | + |
| 41 | + println(students.first()) |
| 42 | + val studentCourseMap = students.map { student -> |
| 43 | + val paidCourses = student.subscribedCourses.filter { |
| 44 | + it.isPaid |
| 45 | + } |
| 46 | + Pair(student, paidCourses) |
| 47 | + }.toMap() |
| 48 | + |
| 49 | + val courseMap = mutableMapOf<Course, Int>() |
| 50 | + studentCourseMap.map { (student, courses) -> |
| 51 | + courses.map { course -> |
| 52 | + if (!courseMap.containsKey(course)) { |
| 53 | + courseMap[course] = 1 |
| 54 | + } else { |
| 55 | + courseMap[course] = courseMap[course]!! + 1 |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return courseMap |
| 61 | + .toList() |
| 62 | + .sortedByDescending { it.second } |
| 63 | + .take(coursesCount) |
| 64 | + .toMap() |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +object Test { |
| 72 | + @JvmStatic |
| 73 | + fun main(array: Array<String>) { |
| 74 | + val university = University(RepoImpl<Student>()); |
| 75 | + println(university.getPaidCoursesWithTheNumbersOfSubscribedStudents(1)); |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments