diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index a8a41b1..ea35cde 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -1,9 +1,5 @@ package ru.otus.homework -/** - * Список натуральных чисел от 1 до n - * @param n Последнее натуральное число в списке - */ class NaturalList(n: Int) : List { override val size: Int = n @@ -31,35 +27,19 @@ class NaturalList(n: Int) : List { override fun lastIndexOf(element: Int): Int = indexOf(element) - /** - * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] - */ - override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") - } + override fun subList(fromIndex: Int, toIndex: Int): List = + this.toList().slice(fromIndex until toIndex) - /** - * Returns true if list contains all numbers in the collection - */ - override fun containsAll(elements: Collection): Boolean { - TODO("Not yet implemented") - } + override fun containsAll(elements: Collection): Boolean = + this.toList().containsAll(elements) override fun toString(): String { return "NaturalList(1..$size)" } - /** - * Функция должна возвращать true, если сравнивается с другой реализацией списка тех же чисел - * Например, NaturalList(5) должен быть равен listOf(1,2,3,4,5) - */ - override fun equals(other: Any?): Boolean = false - - /** - * Функция должна возвращать тот же hash-code, что и список другой реализации тех же чисел - * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() - */ - override fun hashCode(): Int = -1 + override fun equals(other: Any?): Boolean = this.toList() == other + + override fun hashCode(): Int = this.toList().hashCode() } private class NaturalIterator(private val n: Int) : Iterator { @@ -73,7 +53,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 +61,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..f45e636 100644 --- a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt +++ b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt @@ -1,6 +1,10 @@ package ru.otus.homework.mapswap -/** - * Меняет местами ключи и значения - */ -fun Map.swap(): Map = TODO("Доделать swap") \ No newline at end of file +fun Map.swap(): Map { + val invertedMap = mutableMapOf() + + this.forEach { + invertedMap[it.value] = it.key + } + return invertedMap +} \ 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..7a33f58 100644 --- a/src/main/kotlin/ru/otus/homework/persons/persons.kt +++ b/src/main/kotlin/ru/otus/homework/persons/persons.kt @@ -1,13 +1,8 @@ package ru.otus.homework.persons -/** - * Отсортировать список персон по возрасту в порядке убывания - */ -fun List.sortByAge(): List = TODO("Доделать sortByAge") - -/** - * Отсортировать список персон по фамилии - * - Фамилии сортируются по алфавиту в порядке возрастания - * - Если фамилии совпадают, персоны сортируются по имени в порядке возрастания - */ -fun List.sortByName(): List = TODO("Доделать sortBySurname") \ No newline at end of file +fun List.sortByAge(): List = this.sortedByDescending { it.age } + +fun List.sortByName(): List = + this.sortedWith(compareBy { it.surname }.thenBy { it.name }) + + diff --git a/src/test/kotlin/ru/otus/homework/NaturalListTest.kt b/src/test/kotlin/ru/otus/homework/NaturalListTest.kt index 31ecc0c..50a8646 100644 --- a/src/test/kotlin/ru/otus/homework/NaturalListTest.kt +++ b/src/test/kotlin/ru/otus/homework/NaturalListTest.kt @@ -3,7 +3,7 @@ package ru.otus.homework import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test - class NaturalListTest { +class NaturalListTest { @Test fun returnsRequestedSize() {