Created
March 10, 2016 15:27
-
-
Save reitzig/4c988dfcf225c6c637af to your computer and use it in GitHub Desktop.
Weighted ranking of athletes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cf http://cs.stackexchange.com/q/54253/98 | |
// Runnable version: http://ideone.com/4GyKeB | |
class Athlete(val name: String, val endurance : Int, val speed : Int) { | |
def performance(p : Double) = { | |
p * endurance / Athlete.maxEndurance + (1-p) * speed / Athlete.maxSpeed | |
} | |
override def toString = name | |
} | |
object Athlete { | |
val maxEndurance = 64 | |
val maxSpeed = 12 | |
def apply(name : String, endurance : Int, speed : Int) = new Athlete(name, endurance, speed) | |
} | |
object Main extends App { | |
// your code goes here | |
val input = Seq( | |
Athlete("A", 10, 60), | |
Athlete("B", 12, 59), | |
Athlete("C", 6, 64) | |
) | |
println(rank(0.6)(input)) | |
def rank(p : Double)(athletes : Seq[Athlete]) = { | |
athletes.sortBy(_.performance(p)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment