-
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
ДЗ по коллекциям - Трошин #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package com.example.hw2 | ||
|
||
data class City(val title: String) | ||
|
||
data class Product(val name: String, val price: Double) | ||
|
@@ -9,9 +11,6 @@ data class Customer(val name: String, val city: City, val orders: List<Order>) | |
data class Shop(val name: String, val customers: List<Customer>) | ||
|
||
|
||
|
||
|
||
|
||
fun main() { | ||
val city1 = City("Ulsk") | ||
val city2 = City("Piter") | ||
|
@@ -73,37 +72,86 @@ 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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В общем правильно и так, но можно писать в одну строку, если функция маленькая |
||
return customers.map { | ||
it.city | ||
}.toSet() | ||
} | ||
|
||
// Вернуть список клиентов из представленного города | ||
fun Shop.getCustomersFrom(city: City): List<Customer> = listOf() | ||
fun Shop.getCustomersFrom(city: City): List<Customer> { | ||
return customers.filter { | ||
it.city == city | ||
} | ||
} | ||
|
||
// Вернуть true если хоть один клиент из выбранного города | ||
fun Shop.hasCustomerFrom(city: City): Boolean = false | ||
fun Shop.hasCustomerFrom(city: City): Boolean { | ||
return customers.any { | ||
it.city == city | ||
} | ||
} | ||
|
||
// Вернуть количество клментов из выбранного города | ||
fun Shop.countCustomersFrom(city: City): Int = 0 | ||
// Вернуть количество клиентов из выбранного города | ||
fun Shop.countCustomersFrom(city: City): Int { | ||
return customers.count { | ||
it.city == city | ||
} | ||
} | ||
|
||
// Вернуть клиента из выбранного города или null, если нет таких | ||
fun Shop.findAnyCustomerFrom(city: City): Customer? = null | ||
fun Shop.findAnyCustomerFrom(city: City): Customer? { | ||
return customers.firstOrNull { | ||
it.city == city | ||
} | ||
} | ||
|
||
// Вернуть сет всех продуктов заказанных клиентом | ||
fun Customer.getOrderedProducts(): Set<Product> = setOf() | ||
fun Customer.getOrderedProducts(): Set<Product> { | ||
return orders.flatMap { | ||
it.products | ||
}.toSet() | ||
} | ||
|
||
// Отсортировать клиентов по количеству заказов от меньшего к большему | ||
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> = listOf() | ||
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> { | ||
return customers.sortedBy { | ||
it.orders.count() | ||
} | ||
} | ||
|
||
// Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе | ||
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> = mapOf() | ||
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> { | ||
|
||
return customers.groupBy { | ||
it.city.title | ||
}.mapValues { | ||
it.value.toSet() | ||
} | ||
} | ||
|
||
// Вернуть сет клиентов, у которых не доставленных заказов больше чем заказанных | ||
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = setOf() | ||
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> { | ||
return customers.filter { it -> | ||
it.orders.count { it.isDelivered } < it.orders.count { !it.isDelivered } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отлично. Как альтернатива еще partition |
||
}.toSet() | ||
} | ||
|
||
// Вернуть наиболее дорогой продукт из всех доставленных | ||
fun Customer.getMostExpensiveDeliveredProduct(): Product? = null | ||
fun Customer.getMostExpensiveDeliveredProduct(): Product? { | ||
return orders.filter { it.isDelivered }.flatMap { it -> | ||
it.products.sortedBy { it.price } | ||
}.lastOrNull() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ошибка . У тебя сортируются продукты перед добавлением в общую коллекцию.
То в твоем случае наибольшая цена будет 10, а не 100 как должно быть |
||
} | ||
|
||
// Вернуть число - сколько раз был заказан выбранный продукт | ||
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int = 0 | ||
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int { | ||
return customers.flatMap { it -> | ||
it.orders.flatMap { | ||
it.products | ||
} | ||
}.count { it == product } | ||
} |
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.
Это зачем?