Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ppettarrm
Copy link

No description provided.

@vuksa vuksa force-pushed the main branch 2 times, most recently from 9db668d to 189c667 Compare March 27, 2024 16:39
val stack = LinkedList<Char>()
expression.forEach {
when (it) {
in brackets.values -> stack.push(it)
Copy link
Owner

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 and brackets.keys to variables before we start iterating over the chars in the expression.

@@ -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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need an intersect call here.

Its enough to subtract allDrivers - trips.map { it.drivers } to get the result.

}

/**
* 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 }
Copy link
Owner

Choose a reason for hiding this comment

The 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 }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! 👏🏻

TODO("Implement me!!!")
return this.trips
.filter { it.discount != null }
.flatMap { it.passengers }
Copy link
Owner

Choose a reason for hiding this comment

The 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.
The assignment request is to return a list of passengers who had more than half of the trips with the discount.

@vuksa vuksa added the reviewed label Apr 20, 2024
@vuksa vuksa force-pushed the main branch 3 times, most recently from 6106c56 to 8296ece Compare May 20, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants