go - 从给定范围生成组合

标签 go combinations

我正在尝试创建一个能够从给定范围内生成组合的程序。

我开始编辑下面生成组合的代码:

package main

import "fmt"

func nextPassword(n int, c string) func() string {
    r := []rune(c)
    p := make([]rune, n)
    x := make([]int, len(p))
    return func() string {
        p := p[:len(x)]
        for i, xi := range x {
            p[i] = r[xi]
        }
        for i := len(x) - 1; i >= 0; i-- {
            x[i]++
            if x[i] < len(r) {
                break
            }
            x[i] = 0
            if i <= 0 {
                x = x[0:0]
                break
            }
        }
        return string(p)
    }
}

func main() {
    np := nextPassword(2, "ABCDE")
    for {
        pwd := np()
        if len(pwd) == 0 {
            break
        }
        fmt.Println(pwd)
    }
}

这是代码的输出:

AA
AB
AC
AD
AE
BA
BB
BC
BD
BE
CA
CB
CC
CD
CE
DA
DB
DC
DD
DE
EA
EB
EC
ED
EE

这是我编辑的代码:

package main

import "fmt"

const (
    Min = 5
    Max = 10
)

func nextPassword(n int, c string) func() string {
    r := []rune(c)
    p := make([]rune, n)
    x := make([]int, len(p))
    return func() string {
        p := p[:len(x)]
        for i, xi := range x {
            p[i] = r[xi]
        }
        for i := len(x) - 1; i >= 0; i-- {
            x[i]++
            if x[i] < len(r) {
                break
            }
            x[i] = 0
            if i <= 0 {
                x = x[0:0]
                break
            }
        }
        return string(p)
    }
}

func main() {
    cont := 0
    np := nextPassword(2, "ABCDE")
    for {
        pwd := np()
        if len(pwd) == 0 {
            break
        }
        if cont >= Min && cont <= Max{
            fmt.Println(pwd)
        } else if cont > Max{
            break
        }
        cont += 1
    }
}

输出:

BA
BB
BC
BD
BE
CA

我的代码有效,但如果我增加组合的长度并且我的范围从中间开始,程序甚至会生成我不想要的组合(当然这会花费很多时间)。 我该如何解决这个问题?

最佳答案

我真的不喜欢 nextPassword 的写法,所以我做了一个变体。它不是从 0 开始并重复返回下一个值,而是采用一个整数并将其转换为相应的“密码”。例如。 toPassword(0, 2, []rune("ABCDE"))AAtoPassword(5, ...)BA.

从那里开始,很容易循环遍历您想要的任何范围。但我还围绕它编写了一个 nextPassword 包装器,其行为与原始代码中的包装器类似。这个使用 toPassword 并以 n 开头。

此处可运行版本:https://play.golang.org/p/fBo6mx4Mji

代码如下:

package main

import (
    "fmt"
)

func toPassword(n, length int, alphabet []rune) string {
    base := len(alphabet)

    // This will be our output
    result := make([]rune, length)

    // Start filling from the right
    i := length - 1

    // This is essentially a conversion to base-b, where b is
    // the number of possible letters (5 in the case of "ABCDE")
    for n > 0 {
        // Filling from the right, put the right digit mod b
        result[i] = alphabet[n%base]

        // Divide the number by the base so we're ready for
        // the next digit
        n /= base

        // Move to the left
        i -= 1
    }

    // Fill anything that's left with "zeros" (first letter of
    // the alphabet)
    for i >= 0 {
        result[i] = alphabet[0]
        i -= 1
    }

    return string(result)
}

// Convenience function that just returns successive values from
// toPassword starting at start
func nextPassword(start, length int, alphabet []rune) func() string {
    n := start
    return func() string {
        result := toPassword(n, length, alphabet)
        n += 1
        return result
    }
}

func main() {
    for i := 5; i < 11; i++ {
        fmt.Println(toPassword(i, 2, []rune("ABCDE")))
    } // BA, BB, BC, BD, BE, CA

    // Now do the same thing using nextPassword
    np := nextPassword(5, 2, []rune("ABCDE"))
    for i := 0; i < 6; i++ {
        fmt.Println(np())
    } // BA, BB, BC, BD, BE, CA
}

关于go - 从给定范围生成组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44853839/

相关文章:

r - 如何在 R 中迭代生成组合?

go - 从单独的命令/进程共享属性

html - 带有Google V3 API的Golang : Passing alt=media to Export call

Golang 参数函数

php - 无法解决带有变体的php算法

python - 以子列表的形式获取列表的所有可能组合

sorting - 在不创建二级结构的情况下按时间日期字段在 go 1.2 中对结构 slice 进行排序

mysql - 如何预处理使用 sqlx 获取的行?

r - 组合 R 中的列表元素

javascript - 我应该如何用 Javascript 解决这个组合场景?