Refactor assignment 3 and implement alternative Pareto Principle

This commit is contained in:
Martin Berg Alstad
2025-08-01 09:24:57 +02:00
parent 6ef68a5e05
commit 4267323d33
4 changed files with 97 additions and 32 deletions

View File

@ -1,4 +1,4 @@
package taxipark
package assignment.week3.taxipark
/*
* Task #1. Find all the drivers who performed no trips.
@ -49,13 +49,13 @@ fun TaxiPark.findSmartPassengers(): Set<Passenger> = this.allPassengers
* Return any period if many are the most frequent, return `null` if there're no trips.
*/
fun TaxiPark.findTheMostFrequentTripDurationPeriod(): IntRange? {
val maxDuration = this.trips
val (maxDuration, _) = this.trips
.map { trip -> trip.duration / 10 }
.groupBy { it }
.maxByOrNull { it.value.size }
.maxByOrNull { it.value.size } ?: return null
val startRange = maxDuration?.let { it.key * 10 }
return startRange?.let { it.rangeTo(it + 9) }
val startRange = maxDuration * 10
return startRange.rangeTo(startRange + 9)
}
/*