Thursday, October 15, 2009

P12: Decode run-length encoding

Following is the direct solution:

1:  def decode(list:List[(Int,Any)]):List[Any] = {  
2: val b = new ListBuffer[Any]()
3: var temp = list
4: while(!temp.isEmpty){
5: for(i<- 1 to temp.head._1){
6: b+=temp.head._2 }
7: temp = temp.tail
8: }
9: b.toList
10: }
11: println("input List = "+dlist)
12: println(decode(dlist))

The input:
List((6,'a), (2,'c), (4,'e))
The output:
List('a, 'a, 'a, 'a, 'a, 'a, 'c, 'c, 'e, 'e, 'e, 'e)

No comments:

Post a Comment