Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

troshinVA
Copy link

No description provided.

@@ -1,3 +1,5 @@
package com.example.hw2
Copy link
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
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
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
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