开发者

Scala permutation of Factorials

开发者 https://www.devze.com 2023-02-11 08:17 出处:网络
How can I find the n! permutations of certain 开发者_如何学运维letters on Scala?Scala 2.9 RC1:

How can I find the n! permutations of certain 开发者_如何学运维letters on Scala?


Scala 2.9 RC1:

scala> "abc".permutations.toList
res58: List[String] = List(abc, acb, bac, bca, cab, cba)


scala> def permutations[T](xs: List[T]): List[List[T]] = xs match {
     |     case Nil => List(Nil)
     |     case _   => for(x <- xs;ys <- permutations(xs diff List(x))) yield x::ys
     | }
permutations: [T](xs: List[T])List[List[T]]

scala> permutations("abc".toList) foreach println
List(a, b, c)
List(a, c, b)
List(b, a, c)
List(b, c, a)
List(c, a, b)
List(c, b, a)
0

精彩评论

暂无评论...
验证码 换一张
取 消