Implemented not repeating combinations.

This commit is contained in:
k1574 2022-05-19 07:21:26 +05:00
parent 53291a3fa5
commit af7fd31a47

View file

@ -14,15 +14,10 @@ var(
arg0 string arg0 string
delim rune = '\n' delim rune = '\n'
status int = 0 status int = 0
nVal int
chrs []rune
) )
func
usage() {
fmt.Fprintf(os.Stderr, "usage: %s [n_utf8_chars] [len]\n", arg0)
os.Exit(1)
}
func func
Pow(x, p int) int { Pow(x, p int) int {
ret := 1 ret := 1
@ -46,10 +41,29 @@ func
printPins(s []rune, l int) { printPins(s []rune, l int) {
n := Pow(len(s), l) n := Pow(len(s), l)
for i:=0 ; i<n ; i++ { for i:=0 ; i<n ; i++ {
fmt.Println(GetPin(s, l, i)) pin := GetPin(s, l, i)
if Fits([]rune(pin)) {
fmt.Println(GetPin(s, l, i))
}
} }
} }
func
Fits(s []rune) bool {
a := make([]int, len(chrs))
for i, v1 := range chrs {
for _, v2 := range s {
if v1 == v2 {
a[i]++
if a[i] > nVal {
return false
}
}
}
}
return true
}
func func
Run(args []string) int { Run(args []string) int {
arg0 = args[0] arg0 = args[0]
@ -58,14 +72,31 @@ Run(args []string) int {
"Add combinations with less number of chars starting with arg.") "Add combinations with less number of chars starting with arg.")
flagSet.BoolVar(&Lflag, "L", false, flagSet.BoolVar(&Lflag, "L", false,
"Set less number to 1. Overrides l flag.") "Set less number to 1. Overrides l flag.")
flagSet.IntVar(&nVal, "n", 1, "Max repeats of the rune." )
flagSet.Parse(args[1:]) flagSet.Parse(args[1:])
args = flagSet.Args() args = flagSet.Args()
if len(args) < 2 { flagSet.Usage = func() {
usage() fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] <tmpl_chars> [len]\n", arg0)
flagSet.PrintDefaults()
os.Exit(1)
}
var(
err error
n int
)
if len(args) == 0 {
flagSet.Usage()
}
chrs = []rune(args[0])
if len(args) == 2 {
n, err = strconv.Atoi(args[1])
} else if len(args) == 1 {
n = len(chrs)
} }
chrs := []rune(args[0])
n, err := strconv.Atoi(args[1])
if err!=nil { if err!=nil {
log.Fatal(err) log.Fatal(err)
} }
@ -76,7 +107,7 @@ Run(args []string) int {
if lval != 0 { if lval != 0 {
if lval > n { if lval > n {
usage() flagSet.Usage()
} }
for i := lval ; i<=n ; i++ { for i := lval ; i<=n ; i++ {
printPins(chrs, i) printPins(chrs, i)