-
Notifications
You must be signed in to change notification settings - Fork 12
Partial solution for Exercise 3 #13
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
base: main
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,29 +10,56 @@ package exercise3.task4 | |
* Find all the drivers who performed no trips. | ||
*/ | ||
internal fun TaxiPark.findFakeDrivers(): Set<Driver> { | ||
TODO("Implement me!!!") | ||
return allDrivers.filter { driver -> trips.none { it.driver == driver } } | ||
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. This is a correct solution, but its performance is not great because we'll have to do D * T iterations, where D is the number of drivers, and T is the number of trips. The same solution can be done by mapping all the drivers from trips and then deducting all drivers from the list of drivers present in trips. allDrivers - trips.map { it.driver } |
||
.toSet() | ||
} | ||
|
||
/** | ||
* Subtask 2: | ||
* Find all the clients who completed at least the given number of trips. | ||
*/ | ||
internal fun TaxiPark.findFaithfulPassengers(minTrips: Int): Set<Passenger> { | ||
TODO("Implement me!!!") | ||
} | ||
val passengerTripsCount = trips | ||
.flatMap { it.passengers } | ||
.groupingBy { it } | ||
.eachCount() | ||
|
||
return passengerTripsCount | ||
.filter { it.value >= minTrips } | ||
.keys} | ||
|
||
/** | ||
* Subtask 3: | ||
* Find all the passengers, who were taken by a given driver more than once. | ||
*/ | ||
internal fun TaxiPark.findFrequentPassengers(driver: Driver): Set<Passenger> { | ||
TODO("Implement me!!!") | ||
val frequentPassengers = trips | ||
.filter { it.driver == driver } | ||
.flatMap { it.passengers } | ||
.groupingBy { it } | ||
.eachCount() | ||
.filter { it.value > 1 } | ||
|
||
return frequentPassengers.keys | ||
} | ||
|
||
/** | ||
* Subtask 4: | ||
* Find the passengers who had a discount for the majority of their trips. | ||
*/ | ||
internal fun TaxiPark.findSmartPassengers(): Set<Passenger> { | ||
TODO("Implement me!!!") | ||
val passengerDiscountTripsCount = trips | ||
.flatMap { trip -> | ||
trip.passengers.map { passenger -> | ||
passenger to (if (trip.discount != null && trip.discount > 0) 1 else 0) | ||
} | ||
} | ||
.groupBy({ it.first }, { it.second }) | ||
.mapValues { (_, discounts) -> discounts.sum() } | ||
|
||
return passengerDiscountTripsCount | ||
.filter { (_, discounts) -> | ||
discounts >= trips.size / 2 | ||
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. Why are we comparing this with half of all trips? Shouldn't we compare against half of the trips this user made? |
||
} | ||
.keys | ||
} |
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.
Well done! For practice, try solving it with a functional approach. :)