From cce7eddebb9558f6675890dca1e14f302caa01c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D0=B9=20=D0=90=D1=81?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 19 Oct 2024 16:24:31 +0500 Subject: [PATCH 1/4] =?UTF-8?q?1.=20=D0=A1=D0=BE=D1=80=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=BA=D0=B0=20=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BE?= =?UTF-8?q?=D1=87=D0=BD=D0=B8=D0=BA=D0=B0=20=D0=BF=D0=BE=20=D0=B2=D0=BE?= =?UTF-8?q?=D0=B7=D1=80=D0=B0=D1=81=D1=82=D1=83=202.=20=D0=A1=D0=BE=D1=80?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BE=D1=87=D0=BD=D0=B8=D0=BA=D0=B0=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/otus/homework/persons/persons.kt | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/persons/persons.kt b/src/main/kotlin/ru/otus/homework/persons/persons.kt index fc1265d..c0ded3b 100644 --- a/src/main/kotlin/ru/otus/homework/persons/persons.kt +++ b/src/main/kotlin/ru/otus/homework/persons/persons.kt @@ -1,13 +1,7 @@ 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 { + return this.sortedByDescending { it.age } +} +fun List.sortByName(): List { + return this.sortedWith(compareBy { it.surname }.thenBy { it.name }) +} From 04bdcb9caeb30f4ffe528239093aa791009b4c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D0=B9=20=D0=90=D1=81?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 19 Oct 2024 16:25:20 +0500 Subject: [PATCH 2/4] =?UTF-8?q?3.=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B2=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D1=82=20Map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt index 913be37..9072dd5 100644 --- a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt +++ b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt @@ -1,6 +1,4 @@ package ru.otus.homework.mapswap - -/** - * Меняет местами ключи и значения - */ -fun Map.swap(): Map = TODO("Доделать swap") \ No newline at end of file +fun Map.swap(): Map { + return this.entries.associate { (key, value) -> value to key } +} From daf7ef2588768be3421fff23c243ca708e46d2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D0=B9=20=D0=90=D1=81?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 19 Oct 2024 23:44:49 +0500 Subject: [PATCH 3/4] =?UTF-8?q?4.=20=D0=A1=D0=BE=D0=B1=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9=20List?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/otus/homework/NaturalList.kt | 72 +++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index a8a41b1..0917092 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -35,14 +35,17 @@ class NaturalList(n: Int) : List { * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] */ override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") + if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) { + throw IndexOutOfBoundsException("Invalid range: $fromIndex to $toIndex") + } + return (fromIndex + 1 until toIndex + 1).map { it } } /** * Returns true if list contains all numbers in the collection */ override fun containsAll(elements: Collection): Boolean { - TODO("Not yet implemented") + return elements.all { it in 1..size } } override fun toString(): String { @@ -53,39 +56,50 @@ 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 + return this.size == other.size && (1..size).toList() == other + } /** * Функция должна возвращать тот же hash-code, что и список другой реализации тех же чисел * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() */ - override fun hashCode(): Int = -1 -} - -private class NaturalIterator(private val n: Int) : Iterator { - private var index = 0 - override fun hasNext(): Boolean = index < n - override fun next(): Int = if (hasNext()) { - ++index - } else { - throw NoSuchElementException() + override fun hashCode(): Int { + return (1..size).toList().hashCode() } -} -private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIterator { - 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()) { - ++index - } else { - throw NoSuchElementException() + private class NaturalIterator(private val n: Int) : Iterator { + private var index = 0 + override fun hasNext(): Boolean = index < n + override fun next(): Int = if (hasNext()) { + ++index + } else { + throw NoSuchElementException() + } } - override fun nextIndex(): Int = index - override fun previous(): Int = if (hasPrevious()) { - index-- - } else { - throw NoSuchElementException() + + private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIterator { + 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()) { + ++index + } else { + throw NoSuchElementException() + } + + override fun nextIndex(): Int = index + override fun previous(): Int = if (hasPrevious()) { + index-- + } else { + throw NoSuchElementException() + } + + override fun previousIndex(): Int = index } - override fun previousIndex(): Int = index -} \ No newline at end of file +} + + + From 0bb8a86e91b2ce9bd79c1dc70a889399f255649e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D0=B9=20=D0=90=D1=81?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sun, 20 Oct 2024 11:52:14 +0500 Subject: [PATCH 4/4] =?UTF-8?q?4.=20=D0=A1=D0=BE=D0=B1=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9=20List=20=D0=B8=205.=20equals()?= =?UTF-8?q?=20=D0=B8=20hashCode()=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/otus/homework/NaturalList.kt | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index 0917092..7e84fdd 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -40,7 +40,6 @@ class NaturalList(n: Int) : List { } return (fromIndex + 1 until toIndex + 1).map { it } } - /** * Returns true if list contains all numbers in the collection */ @@ -56,50 +55,39 @@ class NaturalList(n: Int) : List { * Функция должна возвращать true, если сравнивается с другой реализацией списка тех же чисел * Например, NaturalList(5) должен быть равен listOf(1,2,3,4,5) */ - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is List<*>) return false - return this.size == other.size && (1..size).toList() == other - } + override fun equals(other: Any?): Boolean = false /** * Функция должна возвращать тот же hash-code, что и список другой реализации тех же чисел * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() */ - override fun hashCode(): Int { - return (1..size).toList().hashCode() - } - - private class NaturalIterator(private val n: Int) : Iterator { - private var index = 0 - override fun hasNext(): Boolean = index < n - override fun next(): Int = if (hasNext()) { - ++index - } else { - throw NoSuchElementException() - } - } - - private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIterator { - 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()) { - ++index - } else { - throw NoSuchElementException() - } - - override fun nextIndex(): Int = index - override fun previous(): Int = if (hasPrevious()) { - index-- - } else { - throw NoSuchElementException() - } + override fun hashCode(): Int = -1 +} - override fun previousIndex(): Int = index +private class NaturalIterator(private val n: Int) : Iterator { + private var index = 0 + override fun hasNext(): Boolean = index < n + override fun next(): Int = if (hasNext()) { + ++index + } else { + throw NoSuchElementException() } } - - +private class NaturalListIterator(private val n: Int, index: Int = 0) : ListIterator { + 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()) { + ++index + } 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