File tree 5 files changed +11
-8
lines changed
Get used to new style/src
5 files changed +11
-8
lines changed Original file line number Diff line number Diff line change 1
1
fun doSomethingStrangeWithCollection (collection : Collection <String >): Collection <String >? {
2
2
3
- val groupsByLength = collection. groupBy { s -> TODO () }
3
+ val groupsByLength = collection.groupBy { s -> s.length }
4
4
5
- val maximumSizeOfGroup = groupsByLength.values.map { group -> TODO () }.max()
5
+ val maximumSizeOfGroup = groupsByLength.values.map { group -> group.size }.max()
6
6
7
- return groupsByLength.values.firstOrNull { group -> TODO () }
7
+ return groupsByLength.values.firstOrNull { group -> group.size == maximumSizeOfGroup }
8
8
}
Original file line number Diff line number Diff line change 1
1
// 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 }
Original file line number Diff line number Diff line change 1
1
// 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 }
3
3
4
4
// 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
+ }
Original file line number Diff line number Diff line change 1
1
// 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 }
Original file line number Diff line number Diff line change 1
1
// Return the sum of prices of all products that a customer has ordered.
2
2
// 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 }
You can’t perform that action at this time.
0 commit comments