diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index a8a41b1..a3d4ad9 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -35,14 +35,14 @@ class NaturalList(n: Int) : List { * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] */ override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") + return this.toList().subList(fromIndex,toIndex) } /** * Returns true if list contains all numbers in the collection */ override fun containsAll(elements: Collection): Boolean { - TODO("Not yet implemented") + return elements.all { contains(it) } } override fun toString(): String { @@ -53,13 +53,17 @@ class NaturalList(n: Int) : List { * Функция должна возвращать true, если сравнивается с другой реализацией списка тех же чисел * Например, NaturalList(5) должен быть равен listOf(1,2,3,4,5) */ - override fun equals(other: Any?): Boolean = false + override fun equals(other: Any?): Boolean { + return this.hashCode() == other.hashCode() + } /** * Функция должна возвращать тот же hash-code, что и список другой реализации тех же чисел * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() */ - override fun hashCode(): Int = -1 + override fun hashCode(): Int { + return this.toList().hashCode() + } } private class NaturalIterator(private val n: Int) : Iterator { @@ -73,7 +77,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 +85,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..d37ce94 100644 --- a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt +++ b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt @@ -1,6 +1,11 @@ package ru.otus.homework.mapswap -/** - * Меняет местами ключи и значения - */ -fun Map.swap(): Map = TODO("Доделать swap") \ No newline at end of file +fun Map.swap(): Map { + + val result = HashMap() + for (key in this.keys) { + val target = this[key] ?: continue + result[target] = key + } + return result +} diff --git a/src/main/kotlin/ru/otus/homework/persons/Person.kt b/src/main/kotlin/ru/otus/homework/persons/Person.kt index ed101dd..81be693 100644 --- a/src/main/kotlin/ru/otus/homework/persons/Person.kt +++ b/src/main/kotlin/ru/otus/homework/persons/Person.kt @@ -1,9 +1,3 @@ package ru.otus.homework.persons -/** - * Запись в справочнике - * @property name Имя - * @property surname Фамилия - * @property age Возраст - */ -data class Person(val name: String, val surname: String, val age: Int) \ No newline at end of file +data class Person(val name: String, val surname: String, val age: Int) diff --git a/src/main/kotlin/ru/otus/homework/persons/persons.kt b/src/main/kotlin/ru/otus/homework/persons/persons.kt index fc1265d..a14ee77 100644 --- a/src/main/kotlin/ru/otus/homework/persons/persons.kt +++ b/src/main/kotlin/ru/otus/homework/persons/persons.kt @@ -1,13 +1,9 @@ package ru.otus.homework.persons -/** - * Отсортировать список персон по возрасту в порядке убывания - */ -fun List.sortByAge(): List = TODO("Доделать sortByAge") +fun main(){ + listOf().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}) \ No newline at end of file