Open
Conversation
homework done
Update main.kt
Antol
approved these changes
Feb 24, 2022
|
|
||
| // Вернуть количество клментов из выбранного города | ||
| fun Shop.countCustomersFrom(city: City): Int = 0 | ||
| fun Shop.countCustomersFrom(city: City): Int = customers.filter { it.city==city }.toSet().size |
Contributor
There was a problem hiding this comment.
Хорошо, но можно просто customers.count{ it.city == city }
| // Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе | ||
| fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> = mapOf() | ||
| fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> { | ||
| val cities = getCitiesCustomersAreFrom() |
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{ (it.orders.filter { !it.isDelivered }.size) > (it.orders.filter { it.isDelivered }.size)}.toSet() |
Contributor
There was a problem hiding this comment.
Ок. Можно было еще через partition, чуть читабельнее (для тех кто знаком с такой функцией конечно)
customers.filter {
val (del, undel) = it.orders.partition { it.isDelivered }
undel.count() > del.count()
}.toSet()
| fun Customer.getMostExpensiveDeliveredProduct(): Product? { | ||
| val products = orders.filter{it.isDelivered}.flatMap {it.products} | ||
| val maxPrice = products.maxOfOrNull { it.price } | ||
| return products.find { it.price==maxPrice } |
Contributor
There was a problem hiding this comment.
Можно без создания дополнительных переменных
orders.filter { it.isDelivered }.flatMap { it.products }.maxByOrNull { it.price }
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.
homework done