Skip to content

Instantly share code, notes, and snippets.

@DiveInto DiveInto/combination.scala
Last active Dec 22, 2015

Embed
What would you like to do?
Functional Programming Principles in Scala week-6 Anagram code snippet
/**
计算全部子集
Input: List( ('a', 2), ('b', 2))
Output:
Nil
List(('a', 1))
List(('a', 2))
List(('b', 1))
List(('b', 2))
List(('a', 1), ('b', 2))
List(('a', 2), ('b', 2))
List(('a', 1), ('b', 1))
List(('a', 2), ('b', 1))
**/
type Occurrences = List[(Char, Int)]
def combination(occ: Occurrences): List[Occurrences] = occ match {
case Nil =>
List(List())
case head :: tail =>
for{i <- (0 to head._2).toList
rest <- combination(tail)
} yield ((head._1, i) :: rest).filter(_._2 != 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.