Open
Conversation
Antol
approved these changes
Feb 24, 2022
|
|
||
| // Вернуть список клиентов из представленного города | ||
| fun Shop.getCustomersFrom(city: City): List<Customer> = listOf() | ||
| fun Shop.getCustomersFrom(city: City): List<Customer> = customers.filter { it.city == city }.toList() |
Contributor
There was a problem hiding this comment.
Последний .toList() тебе просто копию результата делает - не нужно тут это
| // Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе | ||
| fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> = mapOf() | ||
| fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> = | ||
| customers.associate { it.city.title to this.getCustomersFrom(it.city).toSet() } |
Contributor
There was a problem hiding this comment.
Хорошо, только каждый раз еще поиск по всем клиентам. Можно вот так customers.groupBy { it.city.title }.mapValues { it.value.toSet() }
| // Вернуть сет клиентов, у которых не доставленных заказов больше чем заказанных | ||
| fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = setOf() | ||
| fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = customers.filter { customer -> | ||
| customer.orders.filter { !it.isDelivered }.count() > customer.orders.filter { it.isDelivered }.count() }.toSet() |
Contributor
There was a problem hiding this comment.
{ !it.isDelivered } - можно в count передавать, тогда фильтр не нужен будет
| // Вернуть наиболее дорогой продукт из всех доставленных | ||
| fun Customer.getMostExpensiveDeliveredProduct(): Product? = null | ||
| fun Customer.getMostExpensiveDeliveredProduct(): Product? = | ||
| orders.filter { order -> order.isDelivered }.flatMap { it.products }.sortedBy { it.price }.reversed().firstOrNull() |
Contributor
There was a problem hiding this comment.
Накрутила в конце) Можно было просто maxByOrNull { it.price }
| // Вернуть число - сколько раз был заказан выбранный продукт | ||
| fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int = 0 No newline at end of file | ||
| fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int = | ||
| customers.flatMap { it.orders }.flatMap { it.products }.filter { it == product }.count() |
Contributor
There was a problem hiding this comment.
Тоже просто .count { it == product } в конце
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.