Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,45 @@ fun Shop.example(): Int? = customers.map { it.orders }.flatten().find { it.id ==
//TODO

//Преобразовать список клиентов в сет
fun Shop.getSetOfCustomers(): Set<Customer> = setOf()
fun Shop.getSetOfCustomers(): Set<Customer> = customers.toSet()

// Вернуть сет городов в которых проживают клиенты
fun Shop.getCitiesCustomersAreFrom(): Set<City> = setOf()
fun Shop.getCitiesCustomersAreFrom(): Set<City> = customers.map { it.city }.toSet()

// Вернуть список клиентов из представленного города
fun Shop.getCustomersFrom(city: City): List<Customer> = listOf()
fun Shop.getCustomersFrom(city: City): List<Customer> = customers.filter{it.city == city}

// Вернуть true если хоть один клиент из выбранного города
fun Shop.hasCustomerFrom(city: City): Boolean = false
fun Shop.hasCustomerFrom(city: City): Boolean = customers.any{it.city == city}

// Вернуть количество клментов из выбранного города
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 }


// Вернуть клиента из выбранного города или null, если нет таких
fun Shop.findAnyCustomerFrom(city: City): Customer? = null
fun Shop.findAnyCustomerFrom(city: City): Customer? = customers.find{it.city == city}

// Вернуть сет всех продуктов заказанных клиентом
fun Customer.getOrderedProducts(): Set<Product> = setOf()
fun Customer.getOrderedProducts(): Set<Product> = orders.flatMap { it.products }.toSet()

// Отсортировать клиентов по количеству заказов от меньшего к большему
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> = listOf()
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> = customers.sortedBy { it.orders.size }

// Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе
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

return products.find { it.price==mPrice }
}

// Вернуть число - сколько раз был заказан выбранный продукт
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int = 0
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int = customers.flatMap { it.orders }.flatMap { it.products }.count { it==product }