How to find output from input data with solved by Spark and scala
consider,
input : data = 'aaabbbccaabb'
find out
output: 3a3b2c2a2b
Solution:
val data = "aaabbbccaabbzzz"
val fltlist = data.toList
val ans = fltlist.map((_,1)).foldRight(Nil: List[(Char,Int)])((x,y) => y match {
case Nil => List(x)
case ((c,i)::rest) => if (c==x._1)(c,i+1):: rest else x::y
})
var i=0
while (i< (ans.length))
{
print(ans(i)._2+""+ans(i)._1)
i+=1
}
Output:
Comments
Post a Comment