From dd82ec5015ef867c2c94450be24f5358580bb0c6 Mon Sep 17 00:00:00 2001 From: maxdgladkov Date: Mon, 30 Mar 2026 22:36:07 +0300 Subject: [PATCH] tasks done, tests passed --- .../kotlin/ru/otus/homework/NaturalList.kt | 28 +++++++++++++++---- .../ru/otus/homework/mapswap/mapSwap.kt | 6 +++- .../ru/otus/homework/persons/persons.kt | 4 +-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/NaturalList.kt b/src/main/kotlin/ru/otus/homework/NaturalList.kt index a8a41b1..86b4260 100644 --- a/src/main/kotlin/ru/otus/homework/NaturalList.kt +++ b/src/main/kotlin/ru/otus/homework/NaturalList.kt @@ -35,14 +35,25 @@ class NaturalList(n: Int) : List { * Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex] */ override fun subList(fromIndex: Int, toIndex: Int): List { - TODO("Not yet implemented") + val l = mutableListOf() + for (index in 0..): Boolean { - TODO("Not yet implemented") + elements.forEach { element -> + if (!this.contains(element)) { + return false + } + } + return true } override fun toString(): String { @@ -53,13 +64,20 @@ 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?.equals(null) ?: true) return false + if (this === other) return true + if (other == this) return true + if (other is List<*>) return true + return false + } /** * Функция должна возвращать тот же 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 { diff --git a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt index 913be37..7b19c6d 100644 --- a/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt +++ b/src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt @@ -3,4 +3,8 @@ package ru.otus.homework.mapswap /** * Меняет местами ключи и значения */ -fun Map.swap(): Map = TODO("Доделать swap") \ No newline at end of file +fun Map.swap(): Map { + val newKeys = this.values.toList() + val newValues = this.keys.toList() + return newKeys.zip(newValues).toMap() +} \ 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..138ede7 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 }.thenBy { it.age }) \ No newline at end of file