Skip to content

ДЗ по коллекциям - Трошин#5

Open
troshinVA wants to merge 1 commit intoMadBrains:masterfrom
troshinVA:master
Open

ДЗ по коллекциям - Трошин#5
troshinVA wants to merge 1 commit intoMadBrains:masterfrom
troshinVA:master

Conversation

@troshinVA
Copy link
Copy Markdown

No description provided.

@@ -1,3 +1,5 @@
package com.example.hw2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это зачем?


// Вернуть сет городов в которых проживают клиенты
fun Shop.getCitiesCustomersAreFrom(): Set<City> = setOf()
fun Shop.getCitiesCustomersAreFrom(): Set<City> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В общем правильно и так, но можно писать в одну строку, если функция маленькая
fun Shop.getCitiesCustomersAreFrom(): Set<City> = customers.map({it.city}).toSet()

fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = setOf()
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> {
return customers.filter { it ->
it.orders.count { it.isDelivered } < it.orders.count { !it.isDelivered }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отлично. Как альтернатива еще partition

fun Customer.getMostExpensiveDeliveredProduct(): Product? {
return orders.filter { it.isDelivered }.flatMap { it ->
it.products.sortedBy { it.price }
}.lastOrNull()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ошибка . У тебя сортируются продукты перед добавлением в общую коллекцию.
Если будет ситуация например такая (оба заказа доставлены):

[
order {
    product(100),
    product(10),
},
order {
    product(10),
    product(1),
}
]

То в твоем случае наибольшая цена будет 10, а не 100 как должно быть

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants