From af7fd31a478162f5f12be63b1c6e15aa71af157f Mon Sep 17 00:00:00 2001 From: k1574 Date: Thu, 19 May 2022 07:21:26 +0500 Subject: [PATCH] Implemented not repeating combinations. --- pin/pin.go | 57 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/pin/pin.go b/pin/pin.go index fc4f8fc..fa2a213 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -14,15 +14,10 @@ var( arg0 string delim rune = '\n' 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 Pow(x, p int) int { ret := 1 @@ -46,10 +41,29 @@ func printPins(s []rune, l int) { n := Pow(len(s), l) for i:=0 ; i nVal { + return false + } + } + } + } + return true +} + func Run(args []string) int { arg0 = args[0] @@ -58,14 +72,31 @@ Run(args []string) int { "Add combinations with less number of chars starting with arg.") flagSet.BoolVar(&Lflag, "L", false, "Set less number to 1. Overrides l flag.") + flagSet.IntVar(&nVal, "n", 1, "Max repeats of the rune." ) flagSet.Parse(args[1:]) args = flagSet.Args() - if len(args) < 2 { - usage() + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [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 { log.Fatal(err) } @@ -76,7 +107,7 @@ Run(args []string) int { if lval != 0 { if lval > n { - usage() + flagSet.Usage() } for i := lval ; i<=n ; i++ { printPins(chrs, i)