Skip to content

Домашнее задание Collections -Kotlin 4 #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/kotlin/ru/otus/homework/NaturalList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ class NaturalList(n: Int) : List<Int> {
* Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex]
*/
override fun subList(fromIndex: Int, toIndex: Int): List<Int> {
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<Int>): Boolean {
TODO("Not yet implemented")
return elements.all { it in 1..size }
}

override fun toString(): String {
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package ru.otus.homework.mapswap

/**
* Меняет местами ключи и значения
*/
fun <K, V> Map<K, V>.swap(): Map<V, K> = TODO("Доделать swap")
fun <K, V> Map<K, V>.swap(): Map<V, K> {
return this.entries.associate { (key, value) -> value to key }
}
18 changes: 6 additions & 12 deletions src/main/kotlin/ru/otus/homework/persons/persons.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package ru.otus.homework.persons

/**
* Отсортировать список персон по возрасту в порядке убывания
*/
fun List<Person>.sortByAge(): List<Person> = TODO("Доделать sortByAge")

/**
* Отсортировать список персон по фамилии
* - Фамилии сортируются по алфавиту в порядке возрастания
* - Если фамилии совпадают, персоны сортируются по имени в порядке возрастания
*/
fun List<Person>.sortByName(): List<Person> = TODO("Доделать sortBySurname")
fun List<Person>.sortByAge(): List<Person> {
return this.sortedByDescending { it.age }
}
fun List<Person>.sortByName(): List<Person> {
return this.sortedWith(compareBy<Person> { it.surname }.thenBy { it.name })
}