-
Notifications
You must be signed in to change notification settings - Fork 12
Solutions for exercises No.3 #8
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,29 +10,49 @@ package exercise3.task4 | |
* Find all the drivers who performed no trips. | ||
*/ | ||
internal fun TaxiPark.findFakeDrivers(): Set<Driver> { | ||
TODO("Implement me!!!") | ||
return this.allDrivers.minus(this.trips.map { it.driver } | ||
.intersect(this.allDrivers)) | ||
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. We do not need an Its enough to subtract |
||
} | ||
|
||
/** | ||
* 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!!!") | ||
println(this.trips.flatMap { it.passengers } | ||
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. nit: clean up unnecessary println |
||
.groupingBy { it } | ||
.eachCount()) | ||
|
||
return this.trips.flatMap { it.passengers } | ||
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. Well done! 👏🏻 |
||
.groupingBy { it } | ||
.eachCount() | ||
.filter { it.value >= minTrips } | ||
.map { it.key } | ||
.toSet() | ||
} | ||
|
||
/** | ||
* 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!!!") | ||
return this.trips | ||
.filter { it.driver == driver } | ||
.flatMap { it.passengers } | ||
.groupingBy { it } | ||
.eachCount() | ||
.filter { it.value > 1 } | ||
.map { it.key } | ||
.toSet() | ||
} | ||
|
||
/** | ||
* Subtask 4: | ||
* Find the passengers who had a discount for the majority of their trips. | ||
*/ | ||
internal fun TaxiPark.findSmartPassengers(): Set<Passenger> { | ||
TODO("Implement me!!!") | ||
return this.trips | ||
.filter { it.discount != null } | ||
.flatMap { it.passengers } | ||
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. Your solution returns a set of passengers who had at least one trip with the discount. |
||
.toSet() | ||
} |
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.
This is the correct implementation. 👏🏻
One potential nitpick here is that you are creating a new list of values and keys on every item you iterate, which can be improved if we assigned
brackets.values
andbrackets.keys
to variables before we start iterating over the chars in the expression.