How to make a go program recursive

huangapple go评论107阅读模式
英文:

How to make a go program recursive

问题

如何将这个Go程序改为递归程序?我正在通过编写一个游戏数字分析器来学习Go语言。我一直在思考如何做到这一点,但无法找到一个可行的解决方案。这是在Google Playground上的链接:https://play.golang.org/p/lsYjYdEUMN。非常感谢任何帮助。

  1. /*
  2. File record.go
  3. Author: Dan Huckson
  4. Date: 20160120
  5. Purpose: Number analyzer
  6. */
  7. package main
  8. import (
  9. "fmt"
  10. )
  11. type Stats struct {
  12. category map[string]Events
  13. }
  14. type Events struct {
  15. event map[string]*Event
  16. }
  17. type Event struct {
  18. value int64
  19. }
  20. func main() {
  21. winners := [][]int{
  22. {1, 2, 3, 4, 5, 6},
  23. {2, 4, 6, 28, 26, 39},
  24. {1, 4, 9, 10, 26, 39},
  25. {1, 9, 19, 29, 26, 49},
  26. {4, 5, 6, 28, 26, 49}}
  27. keys := []string{"digits1", "digits2", "digits3", "digits4", "digits5", "digits6"}
  28. stats := new(Stats)
  29. stats.category = make(map[string]Events)
  30. for _, key := range keys {
  31. events, ok := stats.category[key]
  32. if !ok {
  33. events = *new(Events)
  34. }
  35. events.event = make(map[string]*Event)
  36. stats.category[key] = events
  37. }
  38. fmt.Println()
  39. for _, winner := range winners {
  40. fmt.Println(winner)
  41. stats.digits1("digits1", winner)
  42. stats.digits2("digits2", winner)
  43. stats.digits3("digits3", winner)
  44. stats.digits4("digits4", winner)
  45. stats.digits5("digits5", winner)
  46. stats.digits6("digits6", winner)
  47. }
  48. }
  49. func (stats *Stats) record(key string, balls string) {
  50. event, ok := stats.category[key].event[balls]
  51. if !ok {
  52. event = new(Event)
  53. stats.category[key].event[balls] = event
  54. }
  55. stats.category[key].event[balls].value += 1
  56. word := ""
  57. if len(balls) > 1 {
  58. word = "Balls"
  59. } else {
  60. word = "Ball"
  61. }
  62. fmt.Printf("%s:%s\tCount:%d\n", word, balls_to_csv(balls), stats.category[key].event[balls].value)
  63. }
  64. func (stats *Stats) digits1(key string, winner []int) {
  65. for i := 0; i < len(winner); i++ {
  66. stats.record(key, string(winner[i]))
  67. }
  68. }
  69. func (stats *Stats) digits2(key string, winner []int) {
  70. for i := 0; i < len(winner)-1; i++ {
  71. for j := i + 1; j < len(winner); j++ {
  72. stats.record(key, string(winner[i])+string(winner[j]))
  73. }
  74. }
  75. }
  76. func (stats *Stats) digits3(key string, winner []int) {
  77. for i := 0; i < len(winner)-2; i++ {
  78. for j := i + 1; j < len(winner)-1; j++ {
  79. for k := j + 1; k < len(winner); k++ {
  80. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k]))
  81. }
  82. }
  83. }
  84. }
  85. func (stats *Stats) digits4(key string, winner []int) {
  86. for i := 0; i < len(winner)-3; i++ {
  87. for j := i + 1; j < len(winner)-2; j++ {
  88. for k := j + 1; k < len(winner)-1; k++ {
  89. for l := k + 1; l < len(winner); l++ {
  90. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l]))
  91. }
  92. }
  93. }
  94. }
  95. }
  96. func (stats *Stats) digits5(key string, winner []int) {
  97. for i := 0; i < len(winner)-4; i++ {
  98. for j := i + 1; j < len(winner)-3; j++ {
  99. for k := j + 1; k < len(winner)-2; k++ {
  100. for l := k + 1; l < len(winner)-1; l++ {
  101. for m := l + 1; m < len(winner); m++ {
  102. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m]))
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. func (stats *Stats) digits6(key string, winner []int) {
  110. for i := 0; i < len(winner)-5; i++ {
  111. for j := i + 1; j < len(winner)-4; j++ {
  112. for k := j + 1; k < len(winner)-3; k++ {
  113. for l := k + 1; l < len(winner)-2; l++ {
  114. for m := l + 1; m < len(winner)-1; m++ {
  115. for n := m + 1; n < len(winner); n++ {
  116. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m])+string(winner[n]))
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. func balls_to_csv(key string) string {
  125. s := ""
  126. length := len(key)
  127. for i := 0; i < length; i++ {
  128. s += fmt.Sprintf("%d,", key[i])
  129. }
  130. return s[:len(s)-1]
  131. }
英文:

How could I make this go program recursive. I'm learning golang by writing a game number analyzer. I've been thinking and thinking on how to do this and can't come up with a working solution. Here is the link in the <a href="https://play.golang.org/p/lsYjYdEUMN">Google Playground.</a> Any help would be greatly appreciated.

  1. /*
  2. File record.go
  3. Author: Dan Huckson
  4. Date: 20160120
  5. Purpose: Number analyzer
  6. */
  7. package main
  8. import (
  9. &quot;fmt&quot;
  10. )
  11. type Stats struct {
  12. category map[string]Events
  13. }
  14. type Events struct {
  15. event map[string]*Event
  16. }
  17. type Event struct {
  18. value int64
  19. }
  20. func main() {
  21. winners := [][]int{
  22. {1, 2, 3, 4, 5, 6},
  23. {2, 4, 6, 28, 26, 39},
  24. {1, 4, 9, 10, 26, 39},
  25. {1, 9, 19, 29, 26, 49},
  26. {4, 5, 6, 28, 26, 49}}
  27. keys := []string{&quot;digits1&quot;, &quot;digits2&quot;, &quot;digits3&quot;, &quot;digits4&quot;, &quot;digits5&quot;, &quot;digits6&quot;}
  28. stats := new(Stats)
  29. stats.category = make(map[string]Events)
  30. for _, key := range keys {
  31. events, ok := stats.category[key]
  32. if !ok {
  33. events = *new(Events)
  34. }
  35. events.event = make(map[string]*Event)
  36. stats.category[key] = events
  37. }
  38. fmt.Println()
  39. for _, winner := range winners {
  40. fmt.Println(winner)
  41. stats.digits1(&quot;digits1&quot;, winner)
  42. stats.digits2(&quot;digits2&quot;, winner)
  43. stats.digits3(&quot;digits3&quot;, winner)
  44. stats.digits4(&quot;digits4&quot;, winner)
  45. stats.digits5(&quot;digits5&quot;, winner)
  46. stats.digits6(&quot;digits6&quot;, winner)
  47. }
  48. }
  49. func (stats *Stats) record(key string, balls string) {
  50. event, ok := stats.category[key].event[balls]
  51. if !ok {
  52. event = new(Event)
  53. stats.category[key].event[balls] = event
  54. }
  55. stats.category[key].event[balls].value += 1
  56. word := &quot;&quot;
  57. if len(balls) &gt; 1 {
  58. word = &quot;Balls&quot;
  59. } else {
  60. word = &quot;Ball&quot;
  61. }
  62. fmt.Printf(&quot;%s:%s\tCount:%d\n&quot;, word, balls_to_csv(balls), stats.category[key].event[balls].value)
  63. }
  64. func (stats *Stats) digits1(key string, winner []int) {
  65. for i := 0; i &lt; len(winner); i++ {
  66. stats.record(key, string(winner[i]))
  67. }
  68. }
  69. func (stats *Stats) digits2(key string, winner []int) {
  70. for i := 0; i &lt; len(winner)-1; i++ {
  71. for j := i + 1; j &lt; len(winner); j++ {
  72. stats.record(key, string(winner[i])+string(winner[j]))
  73. }
  74. }
  75. }
  76. func (stats *Stats) digits3(key string, winner []int) {
  77. for i := 0; i &lt; len(winner)-2; i++ {
  78. for j := i + 1; j &lt; len(winner)-1; j++ {
  79. for k := j + 1; k &lt; len(winner); k++ {
  80. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k]))
  81. }
  82. }
  83. }
  84. }
  85. func (stats *Stats) digits4(key string, winner []int) {
  86. for i := 0; i &lt; len(winner)-3; i++ {
  87. for j := i + 1; j &lt; len(winner)-2; j++ {
  88. for k := j + 1; k &lt; len(winner)-1; k++ {
  89. for l := k + 1; l &lt; len(winner); l++ {
  90. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l]))
  91. }
  92. }
  93. }
  94. }
  95. }
  96. func (stats *Stats) digits5(key string, winner []int) {
  97. for i := 0; i &lt; len(winner)-4; i++ {
  98. for j := i + 1; j &lt; len(winner)-3; j++ {
  99. for k := j + 1; k &lt; len(winner)-2; k++ {
  100. for l := k + 1; l &lt; len(winner)-1; l++ {
  101. for m := l + 1; m &lt; len(winner); m++ {
  102. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m]))
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. func (stats *Stats) digits6(key string, winner []int) {
  110. for i := 0; i &lt; len(winner)-5; i++ {
  111. for j := i + 1; j &lt; len(winner)-4; j++ {
  112. for k := j + 1; k &lt; len(winner)-3; k++ {
  113. for l := k + 1; l &lt; len(winner)-2; l++ {
  114. for m := l + 1; m &lt; len(winner)-1; m++ {
  115. for n := m + 1; n &lt; len(winner); n++ {
  116. stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m])+string(winner[n]))
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. func balls_to_csv(key string) string {
  125. s := &quot;&quot;
  126. length := len(key)
  127. for i := 0; i &lt; length; i++ {
  128. s += fmt.Sprintf(&quot;%d,&quot;, key[i])
  129. }
  130. return s[:len(s)-1]
  131. }

答案1

得分: 1

据我所了解,您想要递归地找到所有中奖号码的组合。例如,

  1. package main
  2. import "fmt"
  3. func combinations(n []int, c []int, ccc [][][]int) [][][]int {
  4. if len(n) == 0 {
  5. return ccc
  6. }
  7. if len(ccc) == 0 {
  8. ccc = make([][][]int, len(n))
  9. }
  10. for i := range n {
  11. cc := make([]int, len(c)+1)
  12. copy(cc, c)
  13. cc[len(cc)-1] = n[i]
  14. ccc[len(cc)-1] = append(ccc[len(cc)-1], cc)
  15. ccc = combinations(n[i+1:], cc, ccc)
  16. }
  17. return ccc
  18. }
  19. func main() {
  20. n := []int{1, 2, 3, 4}
  21. fmt.Println("中奖号码:", n)
  22. fmt.Println()
  23. nw := 0
  24. w := combinations(n, nil, nil)
  25. fmt.Println("中奖票:")
  26. d := " 位数:"
  27. for i := range w {
  28. fmt.Print(i+1, d)
  29. d = " 位数:"
  30. for j := range w[i] {
  31. nw++
  32. fmt.Print(w[i][j], " ")
  33. }
  34. fmt.Println()
  35. }
  36. fmt.Println()
  37. fmt.Println(nw, "个中奖者")
  38. }

输出结果为:

  1. 中奖号码: [1 2 3 4]
  2. 中奖票:
  3. 1 位数: [1] [2] [3] [4]
  4. 2 位数: [1 2] [1 3] [1 4] [2 3] [2 4] [3 4]
  5. 3 位数: [1 2 3] [1 2 4] [1 3 4] [2 3 4]
  6. 4 位数: [1 2 3 4]
  7. 共有 15 个中奖者
  8. 简化后,可以看到递归的形式。
  9. ```go
  10. func combinations(n []int) {
  11. if len(n) == 0 {
  12. return
  13. }
  14. for i := range n {
  15. combinations(n[i+1:])
  16. }
  17. return
  18. }

len(n) == 0 时,递归终止。在 for 循环中,i 递增到 len(n)-1combinations(n[i+1:]) 变为 combinations(n[len(n):]),而 len(n[len(n):]) == 0,这将终止递归。

英文:

As far as I can tell, you want to recursively find all the combinations of winning numbers. For example,

  1. package main
  2. import &quot;fmt&quot;
  3. func combinations(n []int, c []int, ccc [][][]int) [][][]int {
  4. if len(n) == 0 {
  5. return ccc
  6. }
  7. if len(ccc) == 0 {
  8. ccc = make([][][]int, len(n))
  9. }
  10. for i := range n {
  11. cc := make([]int, len(c)+1)
  12. copy(cc, c)
  13. cc[len(cc)-1] = n[i]
  14. ccc[len(cc)-1] = append(ccc[len(cc)-1], cc)
  15. ccc = combinations(n[i+1:], cc, ccc)
  16. }
  17. return ccc
  18. }
  19. func main() {
  20. n := []int{1, 2, 3, 4}
  21. fmt.Println(&quot;winning numbers&quot;, n)
  22. fmt.Println()
  23. nw := 0
  24. w := combinations(n, nil, nil)
  25. fmt.Println(&quot;winning tickets:&quot;)
  26. d := &quot; digit : &quot;
  27. for i := range w {
  28. fmt.Print(i+1, d)
  29. d = &quot; digits: &quot;
  30. for j := range w[i] {
  31. nw++
  32. fmt.Print(w[i][j], &quot; &quot;)
  33. }
  34. fmt.Println()
  35. }
  36. fmt.Println()
  37. fmt.Println(nw, &quot;winners&quot;)
  38. }

Output:

  1. winning numbers [1 2 3 4]
  2. winning tickets:
  3. 1 digit : [1] [2] [3] [4]
  4. 2 digits: [1 2] [1 3] [1 4] [2 3] [2 4] [3 4]
  5. 3 digits: [1 2 3] [1 2 4] [1 3 4] [2 3 4]
  6. 4 digits: [1 2 3 4]
  7. 15 winners

Simplifying, you can see the recursion.

  1. func combinations(n []int) {
  2. if len(n) == 0 {
  3. return
  4. }
  5. for i := range n {
  6. combinations(n[i+1:])
  7. }
  8. return
  9. }

The recursion terminates when len(n) == 0. In the for loop, i increases to len(n)-1, combinations(n[i+1:]) becomes combinations(n[len(n):]), and len(n[len(n):]) == 0, which will terminate the recursion.

答案2

得分: 0

递归不是一种特定于语言的概念。如果你知道什么是递归,并且知道如何在Go中编写函数,那么你可以在Go中编写递归函数。

这是一个在Go中的虚拟递归函数。

  1. func f(n int) int {
  2. if n < 0 {
  3. return 0 // 或者其他操作
  4. }
  5. if n == 0 {
  6. return 1
  7. }
  8. return n * f(n - 1)
  9. }

这是一个在Go中的递归函数,因为:

  1. 它有一个终止条件(基本条件)(n <= 0)
  2. f(n) 依赖于所有 x < n 的 f(x)。
  3. 它是用Go编写的。
英文:

Recursion is not a language specific concept. If you know what is recursion and if you know how to write a function in Go, then you can write a recursive function in Go.

This is a dummy recursive function in Go.

  1. func f(n int) int {
  2. if n &lt; 0 {
  3. return 0 // or something else
  4. }
  5. if n == 0 {
  6. return 1
  7. }
  8. return n * f(n - 1)
  9. }

This is a recursive function in Go, because,

  1. It has a termination (base) condition(n <= 0)
  2. f(n) depends on f(x) for all x < n.
  3. It's written in Go.

huangapple
  • 本文由 发表于 2016年1月25日 11:13:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/34984520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定