How to make a go program recursive

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

How to make a go program recursive

问题

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

/*
File record.go
Author: Dan Huckson
Date: 20160120
Purpose: Number analyzer
*/
package main

import (
	"fmt"
)

type Stats struct {
	category map[string]Events
}

type Events struct {
	event map[string]*Event
}

type Event struct {
	value int64
}

func main() {
	winners := [][]int{
		{1, 2, 3, 4, 5, 6},
		{2, 4, 6, 28, 26, 39},
		{1, 4, 9, 10, 26, 39},
		{1, 9, 19, 29, 26, 49},
		{4, 5, 6, 28, 26, 49}}

	keys := []string{"digits1", "digits2", "digits3", "digits4", "digits5", "digits6"}

	stats := new(Stats)
	stats.category = make(map[string]Events)

	for _, key := range keys {
		events, ok := stats.category[key]
		if !ok {
			events = *new(Events)
		}
		events.event = make(map[string]*Event)
		stats.category[key] = events

	}
	fmt.Println()

	for _, winner := range winners {
		fmt.Println(winner)
		stats.digits1("digits1", winner)
		stats.digits2("digits2", winner)
		stats.digits3("digits3", winner)
		stats.digits4("digits4", winner)
		stats.digits5("digits5", winner)
		stats.digits6("digits6", winner)
	}
}

func (stats *Stats) record(key string, balls string) {

	event, ok := stats.category[key].event[balls]
	if !ok {
		event = new(Event)
		stats.category[key].event[balls] = event
	}
	stats.category[key].event[balls].value += 1

	word := ""
	if len(balls) > 1 {
		word = "Balls"
	} else {
		word = "Ball"
	}

	fmt.Printf("%s:%s\tCount:%d\n", word, balls_to_csv(balls), stats.category[key].event[balls].value)
}

func (stats *Stats) digits1(key string, winner []int) {
	for i := 0; i < len(winner); i++ {
		stats.record(key, string(winner[i]))
	}
}

func (stats *Stats) digits2(key string, winner []int) {
	for i := 0; i < len(winner)-1; i++ {
		for j := i + 1; j < len(winner); j++ {
			stats.record(key, string(winner[i])+string(winner[j]))
		}
	}
}

func (stats *Stats) digits3(key string, winner []int) {
	for i := 0; i < len(winner)-2; i++ {
		for j := i + 1; j < len(winner)-1; j++ {
			for k := j + 1; k < len(winner); k++ {
				stats.record(key, string(winner[i])+string(winner[j])+string(winner[k]))
			}
		}
	}
}

func (stats *Stats) digits4(key string, winner []int) {
	for i := 0; i < len(winner)-3; i++ {
		for j := i + 1; j < len(winner)-2; j++ {
			for k := j + 1; k < len(winner)-1; k++ {
				for l := k + 1; l < len(winner); l++ {
					stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l]))
				}
			}
		}
	}
}

func (stats *Stats) digits5(key string, winner []int) {
	for i := 0; i < len(winner)-4; i++ {
		for j := i + 1; j < len(winner)-3; j++ {
			for k := j + 1; k < len(winner)-2; k++ {
				for l := k + 1; l < len(winner)-1; l++ {
					for m := l + 1; m < len(winner); m++ {
						stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m]))
					}
				}
			}
		}
	}
}

func (stats *Stats) digits6(key string, winner []int) {
	for i := 0; i < len(winner)-5; i++ {
		for j := i + 1; j < len(winner)-4; j++ {
			for k := j + 1; k < len(winner)-3; k++ {
				for l := k + 1; l < len(winner)-2; l++ {
					for m := l + 1; m < len(winner)-1; m++ {
						for n := m + 1; n < len(winner); n++ {
							stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m])+string(winner[n]))
						}
					}
				}
			}
		}
	}
}

func balls_to_csv(key string) string {
	s := ""
	length := len(key)
	for i := 0; i < length; i++ {
		s += fmt.Sprintf("%d,", key[i])
	}
	return s[:len(s)-1]
}
英文:

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.

/*
File record.go
Author: Dan Huckson
Date: 20160120
Purpose: Number analyzer
*/
package main
import (
&quot;fmt&quot;
)
type Stats struct {
category map[string]Events
}
type Events struct {
event map[string]*Event
}
type Event struct {
value int64
}
func main() {
winners := [][]int{
{1, 2, 3, 4, 5, 6},
{2, 4, 6, 28, 26, 39},
{1, 4, 9, 10, 26, 39},
{1, 9, 19, 29, 26, 49},
{4, 5, 6, 28, 26, 49}}
keys := []string{&quot;digits1&quot;, &quot;digits2&quot;, &quot;digits3&quot;, &quot;digits4&quot;, &quot;digits5&quot;, &quot;digits6&quot;}
stats := new(Stats)
stats.category = make(map[string]Events)
for _, key := range keys {
events, ok := stats.category[key]
if !ok {
events = *new(Events)
}
events.event = make(map[string]*Event)
stats.category[key] = events
}
fmt.Println()
for _, winner := range winners {
fmt.Println(winner)
stats.digits1(&quot;digits1&quot;, winner)
stats.digits2(&quot;digits2&quot;, winner)
stats.digits3(&quot;digits3&quot;, winner)
stats.digits4(&quot;digits4&quot;, winner)
stats.digits5(&quot;digits5&quot;, winner)
stats.digits6(&quot;digits6&quot;, winner)
}
}
func (stats *Stats) record(key string, balls string) {
event, ok := stats.category[key].event[balls]
if !ok {
event = new(Event)
stats.category[key].event[balls] = event
}
stats.category[key].event[balls].value += 1
word := &quot;&quot;
if len(balls) &gt; 1 {
word = &quot;Balls&quot;
} else {
word = &quot;Ball&quot;
}
fmt.Printf(&quot;%s:%s\tCount:%d\n&quot;, word, balls_to_csv(balls), stats.category[key].event[balls].value)
}
func (stats *Stats) digits1(key string, winner []int) {
for i := 0; i &lt; len(winner); i++ {
stats.record(key, string(winner[i]))
}
}
func (stats *Stats) digits2(key string, winner []int) {
for i := 0; i &lt; len(winner)-1; i++ {
for j := i + 1; j &lt; len(winner); j++ {
stats.record(key, string(winner[i])+string(winner[j]))
}
}
}
func (stats *Stats) digits3(key string, winner []int) {
for i := 0; i &lt; len(winner)-2; i++ {
for j := i + 1; j &lt; len(winner)-1; j++ {
for k := j + 1; k &lt; len(winner); k++ {
stats.record(key, string(winner[i])+string(winner[j])+string(winner[k]))
}
}
}
}
func (stats *Stats) digits4(key string, winner []int) {
for i := 0; i &lt; len(winner)-3; i++ {
for j := i + 1; j &lt; len(winner)-2; j++ {
for k := j + 1; k &lt; len(winner)-1; k++ {
for l := k + 1; l &lt; len(winner); l++ {
stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l]))
}
}
}
}
}
func (stats *Stats) digits5(key string, winner []int) {
for i := 0; i &lt; len(winner)-4; i++ {
for j := i + 1; j &lt; len(winner)-3; j++ {
for k := j + 1; k &lt; len(winner)-2; k++ {
for l := k + 1; l &lt; len(winner)-1; l++ {
for m := l + 1; m &lt; len(winner); m++ {
stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m]))
}
}
}
}
}
}
func (stats *Stats) digits6(key string, winner []int) {
for i := 0; i &lt; len(winner)-5; i++ {
for j := i + 1; j &lt; len(winner)-4; j++ {
for k := j + 1; k &lt; len(winner)-3; k++ {
for l := k + 1; l &lt; len(winner)-2; l++ {
for m := l + 1; m &lt; len(winner)-1; m++ {
for n := m + 1; n &lt; len(winner); n++ {
stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m])+string(winner[n]))
}
}
}
}
}
}
}
func balls_to_csv(key string) string {
s := &quot;&quot;
length := len(key)
for i := 0; i &lt; length; i++ {
s += fmt.Sprintf(&quot;%d,&quot;, key[i])
}
return s[:len(s)-1]
}

答案1

得分: 1

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

package main

import "fmt"

func combinations(n []int, c []int, ccc [][][]int) [][][]int {
	if len(n) == 0 {
		return ccc
	}
	if len(ccc) == 0 {
		ccc = make([][][]int, len(n))
	}
	for i := range n {
		cc := make([]int, len(c)+1)
		copy(cc, c)
		cc[len(cc)-1] = n[i]
		ccc[len(cc)-1] = append(ccc[len(cc)-1], cc)
		ccc = combinations(n[i+1:], cc, ccc)
	}
	return ccc
}

func main() {
	n := []int{1, 2, 3, 4}
	fmt.Println("中奖号码:", n)
	fmt.Println()
	nw := 0
	w := combinations(n, nil, nil)
	fmt.Println("中奖票:")
	d := " 位数:"
	for i := range w {
		fmt.Print(i+1, d)
		d = " 位数:"
		for j := range w[i] {
			nw++
			fmt.Print(w[i][j], " ")
		}
		fmt.Println()
	}
	fmt.Println()
	fmt.Println(nw, "个中奖者")
}

输出结果为:

中奖号码: [1 2 3 4]
中奖票:
1 位数: [1] [2] [3] [4] 
2 位数: [1 2] [1 3] [1 4] [2 3] [2 4] [3 4] 
3 位数: [1 2 3] [1 2 4] [1 3 4] [2 3 4] 
4 位数: [1 2 3 4] 
共有 15 个中奖者
简化后,可以看到递归的形式。
```go
func combinations(n []int) {
if len(n) == 0 {
return 
}
for i := range n {
combinations(n[i+1:])
}
return 
}

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,

package main
import &quot;fmt&quot;
func combinations(n []int, c []int, ccc [][][]int) [][][]int {
if len(n) == 0 {
return ccc
}
if len(ccc) == 0 {
ccc = make([][][]int, len(n))
}
for i := range n {
cc := make([]int, len(c)+1)
copy(cc, c)
cc[len(cc)-1] = n[i]
ccc[len(cc)-1] = append(ccc[len(cc)-1], cc)
ccc = combinations(n[i+1:], cc, ccc)
}
return ccc
}
func main() {
n := []int{1, 2, 3, 4}
fmt.Println(&quot;winning numbers&quot;, n)
fmt.Println()
nw := 0
w := combinations(n, nil, nil)
fmt.Println(&quot;winning tickets:&quot;)
d := &quot; digit : &quot;
for i := range w {
fmt.Print(i+1, d)
d = &quot; digits: &quot;
for j := range w[i] {
nw++
fmt.Print(w[i][j], &quot; &quot;)
}
fmt.Println()
}
fmt.Println()
fmt.Println(nw, &quot;winners&quot;)
}

Output:

winning numbers [1 2 3 4]
winning tickets:
1 digit : [1] [2] [3] [4] 
2 digits: [1 2] [1 3] [1 4] [2 3] [2 4] [3 4] 
3 digits: [1 2 3] [1 2 4] [1 3 4] [2 3 4] 
4 digits: [1 2 3 4] 
15 winners

Simplifying, you can see the recursion.

func combinations(n []int) {
if len(n) == 0 {
return 
}
for i := range n {
combinations(n[i+1:])
}
return 
}

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中的虚拟递归函数。

func f(n int) int {
if n < 0 {
return 0 // 或者其他操作
}
if n == 0 {
return 1
}
return n * f(n - 1)
}

这是一个在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.

func f(n int) int {
if n &lt; 0 {
return 0 // or something else
}
if n == 0 {
return 1
}
return n * f(n - 1)
}

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:

确定