You are viewing a single comment's thread. Return to all comments →
Scala with recursive approach
... def breakingRecords(scores: Array[Int]): Array[Int] = scores.toList match { case (score :: Nil) => checkScore(ScoreKeeper(score, score), Nil) case (score :: tail) => checkScore(ScoreKeeper(score, score), tail) } def checkScore(keeper:ScoreKeeper, scores: List[Int]):Array[Int] = scores match { case score :: tail => checkScore(keeper.recalc(score), tail ) case Nil => Array(keeper.maxBreaks, keeper.minBreaks) } } case class ScoreKeeper(max:Int, min:Int, maxBreaks:Int=0, minBreaks:Int=0) { def recalc(currentScore: Int): ScoreKeeper = (currentScore > max, currentScore < min) match { case (true, false) => ScoreKeeper(currentScore, min, maxBreaks + 1, minBreaks) case (false, true) => ScoreKeeper(max, currentScore, maxBreaks, minBreaks + 1) case _ => this } }
Seems like cookies are disabled on this browser, please enable them to open this website
Breaking the Records
You are viewing a single comment's thread. Return to all comments →
Scala with recursive approach