【问题标题】:Generate combinations from a given range从给定范围生成组合
【发布时间】:2017-06-30 19:43:37
【问题描述】:

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

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

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函数吗?呃,写这个函数的人要么故意混淆它,要么急需学习有用的变量名。

标签: go combinations


【解决方案1】:

我真的不喜欢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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2013-01-04
    • 2020-02-25
    • 2017-10-05
    • 2013-07-30
    相关资源
    最近更新 更多