如何编写一个函数来打印给定字符串中所有可能的字符串?

发布于 2024-10-18 10:02:22 字数 446 浏览 3 评论 0原文

我正在尝试编写执行以下操作的代码: 1.有一个字符串“abc” 2.它打印出所有可能的字符串: abc, bac, cba, acb, cab, bca

以下是我编写的代码,但它始终给出错误:

def swap(n: Int, source: String): String = {
  val new_array = source.toCharArray
  if (n == 1) {
    new_string
  } else {
    var letter = new_array(n)
    letter = new_array(n-1)
    new_array(n-1) = letter
    var k = new String(new_array)
    swap(n, source)
  }
}

你能告诉我我的错误在哪里吗?

先感谢您。

I am trying to write a code that does the following:
1. there is a string "abc"
2.it prints all the possible strings from it: abc, bac, cba, acb, cab, bca

The following is the code I wrote, however it gives an error all the time:

def swap(n: Int, source: String): String = {
  val new_array = source.toCharArray
  if (n == 1) {
    new_string
  } else {
    var letter = new_array(n)
    letter = new_array(n-1)
    new_array(n-1) = letter
    var k = new String(new_array)
    swap(n, source)
  }
}

Can you please tell me where is my mistake?

Thank you in advance.

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

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

发布评论

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

评论(5

世界和平 2024-10-25 10:02:22
scala> "abc".permutations.toList
res1: List[String] = List(abc, acb, bac, bca, cab, cba)

不过我不会把那个上交。

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

I wouldn't turn that one in though.

走野 2024-10-25 10:02:22

真正明显的错误是你把名字搞混了。

我可以看到 new_array 正在初始化,但随后您尝试使用 new_string,它似乎不是来自任何地方。

The really obvious mistake is you have your names all mixed up.

I can see new_array being initialised, but then you try to use new_string, which doesn't seem to be coming from anywhere.

你与昨日 2024-10-25 10:02:22

一件明显的事情是,您尝试交换两个字母,但顺序错误。应该是 可能

val letter = new_array(n)
new_array(n) = new_array(n-1)
new_array(n-1) = letter

还有其他错误,但因为这是作业......

One obvious thing is that you try to swap two letters, but you got the order wrong. It should be

val letter = new_array(n)
new_array(n) = new_array(n-1)
new_array(n-1) = letter

There might be other errors, but as this is homework...

噩梦成真你也成魔 2024-10-25 10:02:22

正如之前所说,这是一个排列问题。使用 Scala 2.8,您可以编写类似的内容:

def permut[T](l: List[T]): List[List[T]] = {
  def retire(e: T, l: List[T]): List[T] = l match {
    case Nil                => Nil
    case x::xs if e == x    => xs
    case x::xs              => x::retire(e, xs)
  }

  l match {
    case Nil => List(Nil)
    case _   => for ( x <- l; p <- permut(retire(x, l))) yield (x :: p)
  }
}

然后:(

scala> permut("abc" toList) map {_.mkString }                              
res11: List[String] = List(abc, acb, bac, bca, cab, cba)

不过,我不确定我的排列是最佳的。至少,退休可能是尾递归的)

As said before, it's a permutation problem. With Scala 2.8, you could write something like that :

def permut[T](l: List[T]): List[List[T]] = {
  def retire(e: T, l: List[T]): List[T] = l match {
    case Nil                => Nil
    case x::xs if e == x    => xs
    case x::xs              => x::retire(e, xs)
  }

  l match {
    case Nil => List(Nil)
    case _   => for ( x <- l; p <- permut(retire(x, l))) yield (x :: p)
  }
}

Then :

scala> permut("abc" toList) map {_.mkString }                              
res11: List[String] = List(abc, acb, bac, bca, cab, cba)

(I'm not sure my permutation is optimal, though. At least, retire could be tail recursive'd)

放我走吧 2024-10-25 10:02:22

您想要编写一个 next_permutation 函数,其操作可以总结如下:查找数组中按降序排列的最长后缀 - 假设它从索引 m 开始。找出索引 m-1 处的值属于该后缀的位置,然后交换这两个值。然后将后缀从 m 反转到列表末尾,使其按排序顺序排列。

You want to write a next_permutation function, whose operation can be summed up as follows: look for the longest suffix of the array that is in descending order -- let's say it starts at index m. Figure out where the value at index m-1 belongs in that suffix, and swap the two values. Then reverse the suffix from m to the end of the list so that it is in sorted order.

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