The idea is to use above unflatten function to group consecutive duplicates into a list and them map it to length, head pair. Here is the example:
val x = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)
val ulist = unflatten(x)
println(ulist)
println(ulist.map(e=>(e.length,e.head)))
Output:
List(List('a, 'a, 'a, 'a, 'a, 'a), List('b), List('c, 'c), List('d), List('e, 'e, 'e,'e))
List((6,'a), (1,'b), (2,'c), (1,'d), (4,'e))
My thoughts on tackling IT complexity by breaking it into atomic components. and I term it lego oriented solution, LOS for short.
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2009
(13)
-
▼
October
(13)
- P12: Decode run-length encoding
- P11: Modified run-length encoding that includes on...
- Installing Eclipse, Scala and Groovy in Windows 7
- P10: Run-length encoding of a list.
- P09: Pack consecutive duplicates of list elements...
- P08: Eliminate consecutive duplicates of list elem...
- P07: Flatten a list
- P06: Is a list palindrome
- P03 - P05: Find nth element, length, reverse a list
- How to simply sum values in a Map in Scala
- Best Note Taking Tools
- P02: Find the last but one element of a list.
- Over all these years of Software development, I ca...
-
▼
October
(13)
No comments:
Post a Comment