From 4be8a8d599a3aa19ad085df6f46df79ec5060497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B0=D0=B7=D1=83=D0=BD=D0=B8=D0=BD=20=D0=90=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Mon, 27 Apr 2026 14:38:44 +0300 Subject: [PATCH 1/2] All tasks have done --- .../kotlin/ru/otus/homework/NaturalList.kt | 41 +++++++++++++++---- .../ru/otus/homework/mapswap/mapSwap.kt | 2 +- .../ru/otus/homework/persons/persons.kt | 4 +- 3 files changed, 37 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..7a61e68 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -34,16 +34,17 @@ class NaturalList(n: Int) : List { /** * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] */ - override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") + override fun subList(fromIndex: Int, toIndex: Int): List = when{ + fromIndex < 0 -> throw IllegalArgumentException("Parameter fromIndex incorrect: must be non negative") + toIndex > size -> throw IllegalArgumentException("Parameter toIndex incorrect: must less or equal $size") + (toIndex <= fromIndex) -> throw IllegalArgumentException("Parameter toIndex must be higher then parameter fromIndex") + else -> Array(toIndex - fromIndex){fromIndex + it + 1}.toList() } /** * 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.min() > 0 && elements.max() <= size override fun toString(): String { return "NaturalList(1..$size)" @@ -53,13 +54,39 @@ 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(other == this){ + return true + } else if (other !is List<*>){ + return false + } else if((other as List<*>).size != this.size) { + return false + } else { + for(i in 1..this.size){ + if(i != (other as List<*>)[i-1]){ + 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 hash: Int = 1 + + for(i in 1..this.size){ + hash = 31 * hash + i.hashCode() + } + + return hash + } } private class NaturalIterator(private val n: Int) : Iterator { diff --git a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt index 913be37..f3b8a87 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 = this.map { it->Pair(it.value, it.key) }.toMap() diff --git a/src/main/kotlin/ru/otus/homework/persons/persons.kt b/src/main/kotlin/ru/otus/homework/persons/persons.kt index fc1265d..d8d011a 100644 --- a/src/main/kotlin/ru/otus/homework/persons/persons.kt +++ b/src/main/kotlin/ru/otus/homework/persons/persons.kt @@ -3,11 +3,11 @@ package ru.otus.homework.persons /** * Отсортировать список персон по возрасту в порядке убывания */ -fun List.sortByAge(): List = TODO("Доделать sortByAge") +fun List.sortByAge(): List = this.sortedByDescending { it.age } /** * Отсортировать список персон по фамилии * - Фамилии сортируются по алфавиту в порядке возрастания * - Если фамилии совпадают, персоны сортируются по имени в порядке возрастания */ -fun List.sortByName(): List = TODO("Доделать sortBySurname") \ No newline at end of file +fun List.sortByName(): List = this.sortedWith( compareBy({it.surname}, {it.name})) \ No newline at end of file From 8365ce668f082715ed980786e2473177b612e3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B0=D0=B7=D1=83=D0=BD=D0=B8=D0=BD=20=D0=90=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Mon, 27 Apr 2026 14:41:23 +0300 Subject: [PATCH 2/2] Solve redundancy warnings --- src/main/kotlin/ru/otus/homework/NaturalList.kt | 6 +++--- src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index 7a61e68..09be693 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -60,11 +60,11 @@ class NaturalList(n: Int) : List { return true } else if (other !is List<*>){ return false - } else if((other as List<*>).size != this.size) { + } else if(other.size != this.size) { return false } else { for(i in 1..this.size){ - if(i != (other as List<*>)[i-1]){ + if(i != other[i-1]){ return false } } @@ -79,7 +79,7 @@ class NaturalList(n: Int) : List { * Например, NaturalList(5).hashCode() должен быть равен listOf(1,2,3,4,5).hashCode() */ override fun hashCode(): Int{ - var hash: Int = 1 + var hash = 1 for(i in 1..this.size){ hash = 31 * hash + i.hashCode() diff --git a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt index f3b8a87..6c624bc 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 = this.map { it->Pair(it.value, it.key) }.toMap() +fun Map.swap(): Map = this.map { Pair(it.value, it.key) }.toMap()