Skip to content

Commit a7c79dd

Browse files
committed
solved some tasks from Collections part
1 parent a2854c8 commit a7c79dd

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fun doSomethingStrangeWithCollection(collection: Collection<String>): Collection<String>? {
22

3-
val groupsByLength = collection. groupBy { s -> TODO() }
3+
val groupsByLength = collection.groupBy { s -> s.length }
44

5-
val maximumSizeOfGroup = groupsByLength.values.map { group -> TODO() }.max()
5+
val maximumSizeOfGroup = groupsByLength.values.map { group -> group.size }.max()
66

7-
return groupsByLength.values.firstOrNull { group -> TODO() }
7+
return groupsByLength.values.firstOrNull { group -> group.size == maximumSizeOfGroup }
88
}

Collections/GroupBy/src/Task.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Return a map of the customers living in each city
2-
fun Shop.groupCustomersByCity(): Map<City, List<Customer>> = TODO()
2+
fun Shop.groupCustomersByCity(): Map<City, List<Customer>> = customers.groupBy { it.city }

Collections/Max min/src/Task.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Return a customer whose order count is the highest among all customers
2-
fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = TODO()
2+
fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = customers.maxBy { it.orders.size }
33

44
// Return the most expensive product which has been ordered
5-
fun Customer.getMostExpensiveOrderedProduct(): Product? = TODO()
5+
fun Customer.getMostExpensiveOrderedProduct(): Product? {
6+
val products = orders.flatMap { it.products }
7+
return products.maxBy { it.price }
8+
}

Collections/Sort/src/Task.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Return a list of customers, sorted by the ascending number of orders they made
2-
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> = TODO()
2+
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> = customers.sortedBy { it.orders.size }

Collections/Sum/src/Task.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Return the sum of prices of all products that a customer has ordered.
22
// Note: the customer may order the same product for several times.
3-
fun Customer.getTotalOrderPrice(): Double = TODO()
3+
fun Customer.getTotalOrderPrice(): Double = orders.flatMap { it.products }.sumByDouble { it.price }

0 commit comments

Comments
 (0)