Skip to content
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
41 changes: 34 additions & 7 deletions src/main/kotlin/ru/otus/homework/NaturalList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ class NaturalList(n: Int) : List<Int> {
/**
* Вернуть под-список этого списка, включая [fromIndex] и НЕ включая [toIndex]
*/
override fun subList(fromIndex: Int, toIndex: Int): List<Int> {
TODO("Not yet implemented")
override fun subList(fromIndex: Int, toIndex: Int): List<Int> = 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<Int>): Boolean {
TODO("Not yet implemented")
}
override fun containsAll(elements: Collection<Int>): Boolean = elements.min() > 0 && elements.max() <= size

override fun toString(): String {
return "NaturalList(1..$size)"
Expand All @@ -53,13 +54,39 @@ class NaturalList(n: Int) : List<Int> {
* Функция должна возвращать 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.size != this.size) {
return false
} else {
for(i in 1..this.size){
if(i != other[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 = 1

for(i in 1..this.size){
hash = 31 * hash + i.hashCode()
}

return hash
}
}

private class NaturalIterator(private val n: Int) : Iterator<Int> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ru/otus/homework/mapswap/mapSwap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,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> = this.map { Pair(it.value, it.key) }.toMap()
4 changes: 2 additions & 2 deletions src/main/kotlin/ru/otus/homework/persons/persons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package ru.otus.homework.persons
/**
* Отсортировать список персон по возрасту в порядке убывания
*/
fun List<Person>.sortByAge(): List<Person> = TODO("Доделать sortByAge")
fun List<Person>.sortByAge(): List<Person> = this.sortedByDescending { it.age }

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