diff --git a/build.gradle b/build.gradle index e145486..260533f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'org.jetbrains.kotlin.jvm' version '1.9.10' + id 'org.jetbrains.kotlin.jvm' version '2.1.10' } test { @@ -16,4 +16,5 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib" implementation 'org.junit.jupiter:junit-jupiter:5.8.1' + implementation "org.jetbrains.kotlin:kotlin-reflect:2.1.10" } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index a8a41b1..373d533 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -35,31 +35,47 @@ class NaturalList(n: Int) : List { * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] */ override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") + return filterIndexed { index, _ -> index in fromIndex until toIndex } } /** * Returns true if list contains all numbers in the collection */ override fun containsAll(elements: Collection): Boolean { - TODO("Not yet implemented") + return elements.all { this.contains(it) } } override fun toString(): String { - return "NaturalList(1..$size)" + return joinToString(prefix = "[", postfix = "]", separator = ", ") } /** * Функция должна возвращать true, если сравнивается с другой реализацией списка тех же чисел * Например, NaturalList(5) должен быть равен listOf(1,2,3,4,5) */ - override fun equals(other: Any?): Boolean = false + override fun equals(other: Any?): Boolean { + if (other == null) return false + if (this === other) return true + if (other is List<*> && (other as List<*>)[0] is Int && (other as List<*>).size == size) { + containsAll(other as List<*>) + return all { (other as List<*>).contains(it) } + } + return false + } + + private fun naturalList() = this /** * Функция должна возвращать тот же hash-code, что и список другой реализации тех же чисел * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() */ - override fun hashCode(): Int = -1 + override fun hashCode(): Int { + var hashCode = 1 + for (i in this) { + hashCode = 31 * hashCode + (i.hashCode()) + } + return hashCode + } } private class NaturalIterator(private val n: Int) : Iterator { @@ -73,7 +89,7 @@ private class NaturalIterator(private val n: Int) : Iterator { } private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIterator { - private var index:Int = index.coerceIn(0, n - 1) + private var index: Int = index.coerceIn(0, n - 1) override fun hasNext(): Boolean = index < n override fun hasPrevious(): Boolean = index > 0 override fun next(): Int = if (hasNext()) { @@ -81,11 +97,13 @@ private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIter } else { throw NoSuchElementException() } + override fun nextIndex(): Int = index override fun previous(): Int = if (hasPrevious()) { index-- } else { throw NoSuchElementException() } + override fun previousIndex(): Int = index } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt index 913be37..3614c9c 100644 --- a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt +++ b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt @@ -3,4 +3,4 @@ package ru.otus.homework.mapswap /** * Меняет местами ключи и значения */ -fun Map.swap(): Map = TODO("Доделать swap") \ No newline at end of file +fun Map.swap(): Map = map { it.value to it.key }.toMap() \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/persons/persons.kt b/src/main/kotlin/ru/otus/homework/persons/persons.kt index fc1265d..30eb9d9 100644 --- a/src/main/kotlin/ru/otus/homework/persons/persons.kt +++ b/src/main/kotlin/ru/otus/homework/persons/persons.kt @@ -3,11 +3,13 @@ package ru.otus.homework.persons /** * Отсортировать список персон по возрасту в порядке убывания */ -fun List.sortByAge(): List = TODO("Доделать sortByAge") +fun List.sortByAge(): List = sortedByDescending { it.age } /** * Отсортировать список персон по фамилии * - Фамилии сортируются по алфавиту в порядке возрастания * - Если фамилии совпадают, персоны сортируются по имени в порядке возрастания */ -fun List.sortByName(): List = TODO("Доделать sortBySurname") \ No newline at end of file +fun List.sortByName(): List { + return sortedWith(compareBy({ it.surname }, { it.name })) +}