pin: simple implementation added.

This commit is contained in:
k1574 2020-08-30 02:46:11 +05:00
parent 71adfdbf45
commit f3a63fb85b
2 changed files with 69 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import(
"fmt" "fmt"
"os" "os"
"github.com/k1574/utf8tmpl/tmpl" "github.com/k1574/utf8tmpl/tmpl"
"github.com/k1574/utf8tmpl/pin"
) )
func main() { func main() {
@ -15,6 +16,7 @@ func main() {
utilsMap := map[string] interface{} { utilsMap := map[string] interface{} {
"tmpl" : tmpl.Run, "tmpl" : tmpl.Run,
"pin" : pin.Run,
} }
if len(os.Args)<2 { if len(os.Args)<2 {

67
pin/pin.go Normal file
View file

@ -0,0 +1,67 @@
package pin
import(
"os"
"fmt"
"strconv"
"log"
)
var(
arg0 string
delim rune = '\n'
status int = 0
)
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
for i:=0 ; i<p ; i++ {
ret *= x
}
return ret
}
func
GetPin(s []rune, l int, i int) string {
ret := ""
slen := len(s)
for j:=0 ; j<l ; j++ {
ret += string(s[ ( i/Pow(slen, j) ) % slen] )
}
return ret
}
func
printPins(s []rune, l int) {
n := Pow(len(s), l)
for i:=0 ; i<n ; i++ {
fmt.Println(GetPin(s, l, i))
}
}
func
Run(args []string) int {
arg0 = args[0]
if len(args) < 3 {
usage()
}
chrs := []rune(args[1])
n, err := strconv.Atoi(args[2])
if err!=nil {
log.Fatal(err)
}
printPins(chrs, n)
return status
}