-
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
homework done #1
base: master
Are you sure you want to change the base?
Conversation
homework done
Update main.kt
|
||
// Вернуть количество клментов из выбранного города | ||
fun Shop.countCustomersFrom(city: City): Int = 0 | ||
fun Shop.countCustomersFrom(city: City): Int = customers.filter { it.city==city }.toSet().size |
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.
Хорошо, но можно просто customers.count{ it.city == city }
|
||
// Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе | ||
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> = mapOf() | ||
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> { | ||
val cities = getCitiesCustomersAreFrom() |
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.
Интересно, не подумал что можно другие функцию использовать, но не очень по оптимальности решение получается, много лишнего. Так проще
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() |
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, чуть читабельнее (для тех кто знаком с такой функцией конечно)
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 } |
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.
Можно без создания дополнительных переменных
orders.filter { it.isDelivered }.flatMap { it.products }.maxByOrNull { it.price }
homework done