-
-
Save mauricioszabo/2580938 to your computer and use it in GitHub Desktop.
Convert in less than 30 lines
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
val before = List( | |
List('A', 'B', 'C'), | |
List('A', 'C', 'E'), | |
List('E', 'F', 'D'), | |
List('D', 'A', 'J'), | |
List('E', 'D', 'J') | |
) | |
val expected = List( | |
List('A', 'B', 1), | |
List('A', 'C', 2), | |
List('A', 'D', 1), | |
List('A', 'E', 1), | |
List('A', 'J', 1), | |
List('B', 'C', 1), | |
List('C', 'E', 1), | |
List('D', 'E', 2), | |
List('D', 'F', 1), | |
List('D', 'J', 2), | |
List('E', 'F', 1), | |
List('E', 'J', 1) | |
) | |
val map = scala.collection.mutable.Map[List[Char], Int]() | |
before.foreach { row => | |
(row :+ row(0)).sliding(2).foreach { e => | |
map(e.sorted) = map.getOrElse(e.sorted, 0) + 1 | |
} | |
} | |
val after = map.map { case(l, e) => l :+ e }.toList.sortBy { case v:List[Char] => v(0).toByte * 100 + v(1).toByte } | |
assert(expected == after, "List is not what we spected to be: "+after) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In scala, using "List".
It's shorter, but issues a warning. To remove the warning is to give it 3 more lines at least...