使用 scala 进行重复的所有排列

发布于 2024-12-05 09:11:42 字数 651 浏览 1 评论 0原文

我正在寻找 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 是一个类的数组成员,其中包含要组合的所有符号。

I am looking for the scala way to give all permutations without repetitions. I know there are some postings on this site already but they seem to have a slightly different problem.

I am searching for all permutations with repetitions.
For example:

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

Should yield:

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')

I am sorry if my problem is already solved but I was not able to find it.

Thanks in advance.

EDIT:

My own approach (doesn't compile):

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 is an array member of a class which contains all the symbols to be combined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

書生途 2024-12-12 09:11:42

与斯卡拉兹:

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))

With 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))
云仙小弟 2024-12-12 09:11:42
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
    }
}
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
    }
}
旧竹 2024-12-12 09:11:42

这应该有效:

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

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

This should work:

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

(input ++ input ++ input) combinations(3) toList
许你一世情深 2024-12-12 09:11:42
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 但它就在那里,以便您可以看到结果。

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)

And if you wanted the resulting permutations:

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)

You can leave out the toList but it's there so you can see the results.

ぺ禁宫浮华殁 2024-12-12 09:11:42

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

myList = List("A", "C", "G")
for {
  i <- myList
  j <- myList
  k <- myList
} yield List(i,j,k)

(这是以下映射组合的语法糖:

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

Scala 编译器将上面的 for 表达式转换为它。)

It seems no one has suggested the easiest---or, at least, easiest to read---solution. It is

myList = List("A", "C", "G")
for {
  i <- myList
  j <- myList
  k <- myList
} yield List(i,j,k)

(This is syntactic sugar for the following composition of maps:

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

to which the Scala compiler translates the above for expression.)

世界等同你 2024-12-12 09:11:42

在 ScalaZ 7 中

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

In ScalaZ 7

import scalaz._
import Scalaz._
def combinine[T](l: List[T]) = l.replicateM(l.size)
野生奥特曼 2024-12-12 09:11:42

只是从 @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))

Just making a more generic answers, from @opyate and @monnef:

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

This will generate the permutation with repetition with size 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))

and

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文