Skip to content

completed.#3

Open
chehmakin wants to merge 1 commit intoMadBrains:masterfrom
chehmakin:master
Open

completed.#3
chehmakin wants to merge 1 commit intoMadBrains:masterfrom
chehmakin:master

Conversation

@chehmakin
Copy link
Copy Markdown

No description provided.


// Вернуть количество клментов из выбранного города
fun Shop.countCustomersFrom(city: City): Int = 0
fun Shop.countCustomersFrom(city: City): Int = customers.filter{it.city == city}.toSet().size
Copy link
Copy Markdown
Contributor

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()
return cities.associate { it.title to getCustomersFrom(it).toSet() }
Copy link
Copy Markdown
Contributor

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()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно через val (del, undel) = it.orders.partition { it.isDelivered }

fun Customer.getMostExpensiveDeliveredProduct(): Product? = null
fun Customer.getMostExpensiveDeliveredProduct(): Product? {
val products = orders.filter{it.isDelivered}.flatMap {it.products}
val mPrice = products.maxOfOrNull { it.price }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxByOrNull надо было использовать - сразу получил бы Product

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.

2 participants