所有使用scala重复的排列

时间:2011-09-19 17:15:05

标签: scala combinations combinatorics repeat

我正在寻找scala方式来给出所有排列而不重复。我知道这个网站上已有一些帖子,但它们似乎有一个稍微不同的问题。

我正在寻找重复的所有排列。 例如:

combine(List('A','C','G'))

应该屈服:

List(List('A'.'A','A'),List('A'.'A','C'),List('A'.'A','G'),List('A'.'C','A'),
List('A'.'C',''C), ... List('G'.'G','G')

如果我的问题已经解决但我无法找到它,我很抱歉。

提前致谢。

编辑:

我自己的方法(不编译):

def combine(size: Int = sym.length) : List[List[T]] = {
  size match {
    case 0 => List()
    case 1 => sym.toList.map(List(_))
    case _ => for (el <- sym) yield el :: combine(size-1)
  }
}

sym是一个类的数组成员,它包含要组合的所有符号。

7 个答案:

答案 0 :(得分:12)

使用Scalaz:

scala> import scalaz._
import scalaz._

scala> import Scalaz._
import Scalaz._

scala> def combine[A](xs: List[A]): List[List[A]] = {
     |   xs.replicate[List](xs.size).sequence
     | }
combine: [A](xs: List[A])List[List[A]]

scala> combine(List('A', 'C', 'G'))
res47: List[List[Char]] = List(List(A, A, A), List(A, A, C), List(A, A, G), List
(A, C, A), List(A, C, C), List(A, C, G), List(A, G, A), List(A, G, C), List(A, G
, G), List(C, A, A), List(C, A, C), List(C, A, G), List(C, C, A), List(C, C, C),
 List(C, C, G), List(C, G, A), List(C, G, C), List(C, G, G), List(G, A, A), List
(G, A, C), List(G, A, G), List(G, C, A), List(G, C, C), List(G, C, G), List(G, G
, A), List(G, G, C), List(G, G, G))

答案 1 :(得分:8)

def combinations(size: Int = sym.length) : List[List[T]] = {
    if (size == 0)
        List(List())
    else {
        for {
            x  <- sym.toList
            xs <- combinations(size-1)
        } yield x :: xs
    }
}

答案 2 :(得分:8)

这应该有效:

val input = List('A','C','G')

(input ++ input ++ input) combinations(3) toList

答案 3 :(得分:3)

scala> def comb(s:String)=(s * s.length).combinations(s.length)
comb: (s: String)Iterator[String]

scala> comb("ACG").toList
res16: List[String] = List(AAA, AAC, AAG, ACC, ACG, AGG, CCC, CCG, CGG, GGG)

如果你想要得到的排列:

scala> comb("ACG").flatMap(_.toSeq.permutations.toList).toList
res11: List[Seq[Char]] = List(AAA, AAC, ACA, CAA, AAG, AGA, GAA, ACC, CAC, CCA, ACG, AGC, CAG, CGA, GAC, GCA, AGG, GAG, GGA, CCC, CCG, CGC, GCC, CGG, GCG, GGC, GGG)

你可以省略toList,但它就在那里你可以看到结果。

答案 4 :(得分:1)

似乎没有人建议最简单的 - 或者,至少是最容易阅读的 - 解决方案。它是

myList.flatMap(i => myList.flatMap(j => myList.map(k => List(i,j,k))))

(这是以下地图构成的语法糖:

for

Scala编译器将上述var grid = [1,2,3]; var gridRows = ["O","O","O"]; for (var i = 0; i < grid.length; i++) { for(var j = 0; j < grid.length; j++) { grid[i] = JSON.parse(JSON.stringify(gridRows)); } } 表达式转换为。)

答案 5 :(得分:0)

在ScalaZ 7中

import scalaz._
import Scalaz._
def combinine[T](l: List[T]) = l.replicateM(l.size)

答案 6 :(得分:0)

从@opyate和@monnef那里得到更通用的答案:

// considering that we want a permutation_size
List.fill(permutation_size)(input).flatten.combinations(permutation_size).toList

这将生成重复大小permutation_size的排列:

val a = List.fill(2)(List("A","B","C")).flatten.combinations(2).toList
a: List[List[String]] = List(List(A, A), List(A, B), List(A, C), List(B, B), List(B, C), List(C, C))

val a = List.fill(3)(List("A","B","C")).flatten.combinations(3).toList
a: List[List[String]] = List(List(A, A, A), List(A, A, B), List(A, A, C), List(A, B, B), List(A, B, C), List(A, C, C), List(B, B, B), List(B, B, C), List(B, C, C), List(C, C, C))
相关问题