-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: master
Are you sure you want to change the base?
Conversation
@@ -1,3 +1,5 @@ | |||
package com.example.hw2 |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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 } |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 как должно быть
No description provided.