Created
April 29, 2021 00:31
-
-
Save daviscale/0879d474ccaaba6baaf933ddb3708578 to your computer and use it in GitHub Desktop.
This file contains 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
def makeNextSequence( | |
previousSeq: List[Int], | |
currentSubSeq: List[Int] = List.empty, | |
accumulatedSeq: List[List[Int]] = List.empty | |
): List[Int] = { | |
previousSeq match { | |
case head :: tail if currentSubSeq.isEmpty => | |
makeNextSequence(tail, List(head), accumulatedSeq) | |
case head :: tail if head != currentSubSeq.head => | |
makeNextSequence(tail, List(head), accumulatedSeq :+ currentSubSeq) | |
case head :: tail if head == currentSubSeq.head => | |
makeNextSequence(tail, currentSubSeq :+ head, accumulatedSeq) | |
case Nil => | |
val finalAccumulated = accumulatedSeq :+ currentSubSeq | |
val nextSeq = finalAccumulated flatMap { subSeq => | |
List(subSeq.size, subSeq.head) | |
} | |
println(nextSeq.mkString(" ")) | |
nextSeq | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's this function in action on the Scala console: