From 9cdf997d8836a6b329d185856561fc475e7bcdbc Mon Sep 17 00:00:00 2001 From: Andrusov Date: Tue, 24 Sep 2024 19:20:41 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=94=D0=97=20Kotlin-4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/otus/homework/NaturalList.kt | 34 +++++++++++++++---- .../ru/otus/homework/mapswap/mapSwap.kt | 2 +- .../ru/otus/homework/persons/persons.kt | 6 ++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index a8a41b1..cc1db4e 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -35,15 +35,17 @@ class NaturalList(n: Int) : List { * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] */ override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") + require(fromIndex >= 0) + require(fromIndex <= toIndex) + require(toIndex <= size) + + return arrayListOf(fromIndex + 1, 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 = elements.max() <= size override fun toString(): String { return "NaturalList(1..$size)" @@ -53,13 +55,31 @@ 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 { + if (this === other) return true + if (other !is List<*>) return false + if (size != other.size) return false + + for (i in 0 until size) { + if (other[i] != this[i]) return false + } + + return true + } /** * Функция должна возвращать тот же hash-code, что и список другой реализации тех же чисел * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() */ - override fun hashCode(): Int = -1 + override fun hashCode(): Int { + var result = 1; + + for (i in this) { + result = 31 * result + i.hashCode() + } + + return result + } } private class NaturalIterator(private val n: Int) : Iterator { @@ -88,4 +108,4 @@ private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIter 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..45673a8 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() = this.entries.associate { it.value to it.key } diff --git a/src/main/kotlin/ru/otus/homework/persons/persons.kt b/src/main/kotlin/ru/otus/homework/persons/persons.kt index fc1265d..5e7e93e 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 = sortedBy { it.age }.reversed() /** * Отсортировать список персон по фамилии * - Фамилии сортируются по алфавиту в порядке возрастания * - Если фамилии совпадают, персоны сортируются по имени в порядке возрастания */ -fun List.sortByName(): List = TODO("Доделать sortBySurname") \ No newline at end of file +fun List.sortByName(): List = this.sortedWith( + compareBy { it.surname } + .thenBy { it.name }) From 78d48143ac6c4035cb2f84315da722fbf658f512 Mon Sep 17 00:00:00 2001 From: Andrusov Date: Wed, 25 Sep 2024 19:29:32 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/ru/otus/homework/NaturalList.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index cc1db4e..679dd65 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -39,13 +39,17 @@ class NaturalList(n: Int) : List { require(fromIndex <= toIndex) require(toIndex <= size) - return arrayListOf(fromIndex + 1, toIndex) + return (fromIndex + 1..toIndex).toList() } /** * Returns true if list contains all numbers in the collection */ - override fun containsAll(elements: Collection): Boolean = elements.max() <= size + override fun containsAll(elements: Collection): Boolean { + require(elements.min() >= 1) + + return elements.max() <= size + } override fun toString(): String { return "NaturalList(1..$size)"