上机编程认证02---验证密码
作者:互联网
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
* Description: 上机编程认证
* Note: 缺省代码仅供参考,可自行决定使用、修改或删除
* 只能import Go标准库
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
// 字母
func checkPasswdRule001(pwd byte) bool{
if pwd < 123 && pwd > 64 {
return true
}
return false
}
//数字
func checkPasswdRule002(pwd byte) bool{
if pwd < 58 && pwd > 47 {
return true
}
return false
}
//特殊字符
func checkPasswdRule003(pwd byte) bool{
if pwd < 48 && pwd > 31 {
return true
}
if pwd < 65 && pwd > 57 {
return true
}
if pwd < 127 && pwd > 122 {
return true
}
return false
}
func checkPasswdRule01(passwd string) bool {
nums := 0
totalLen := len(passwd)
for i:= 0;i<totalLen;i++{
if checkPasswdRule001(passwd[i]){
nums ++
}
if checkPasswdRule002(passwd[i]){
nums ++
}
if checkPasswdRule003(passwd[i]){
nums ++
}
}
if nums >= 2 {
return true
}
return false
}
func checkPasswdRule03(passwd string) bool {
if len(passwd) < 8 {
return false
}
return true
}
var passwdMap = make(map[string]int,5)
func checkPasswdRule02(passwd string,i int) bool {
if _,ok := passwdMap[passwd]; ok{
delete(passwdMap,passwd)
return false
} else {
if len(passwdMap) < 5 {
if _,ok := passwdMap[passwd];!ok{
passwdMap[passwd] = i
}
}
}
return true
}
func checkPasswdRule(passwd string,i int) bool {
result01 := checkPasswdRule01(passwd)
result03 := checkPasswdRule03(passwd)
result02 := checkPasswdRule02(passwd,i)
return result01 && result03 && result02
}
func checkPasswords(inputArray []string) []int {
result := make([]int, 0)
totalPasswdLens := len(inputArray)
for i:= 0;i < totalPasswdLens;i++ {
//得到每个pwd
passwd := inputArray[i]
if checkPasswdRule(passwd,i) {
result = append(result,1)
} else {
result = append(result,0)
}
}
return result
}
func main() {
var n int
if _, err := fmt.Scanf("%d", &n); err != nil {
return
}
inputArray := make([]string, n)
reader := bufio.NewReader(os.Stdin)
for i := 0; i < n; i++ {
pwd, err := readStrLine(reader)
if err != nil {
return
}
inputArray[i] = pwd
}
result := checkPasswords(inputArray)
fmt.Print("[")
for i, val := range result {
fmt.Print(val)
if i != len(result)-1 {
fmt.Print(" ")
}
}
fmt.Print("]")
}
func readStrLine(reader *bufio.Reader) (string, error) {
lineBuf, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
return "", err
}
str := strings.TrimRight(lineBuf, "\r\n")
return str, nil
}
//test----
package main
import (
"fmt"
"strconv"
"testing"
)
type testStruct1 = struct {
inputStr []string
want string
}
func TestSomeFunction(t *testing.T) {
tests1 := []testStruct1{
{inputStr: []string{
"323sdsd/",
"323sdsd-",
"323sdsd=",
"323sdsd]",
"323sdsd[",
"323sdsd/",
"323sdsd,",
"323sdsd/",
},want: "11111011"},
}
for _,tt := range tests1 {
got := checkPasswords(tt.inputStr)
gotstr := ""
for _, val := range got {
gotstr += strconv.Itoa(val)
}
fmt.Println(got,tt.want)
if gotstr != tt.want {
t.Errorf("checkPasswords(%v) = %v, want %v", tt.inputStr, gotstr, tt.want)
}
}
}
标签:02,return,string,上机,passwd,---,pwd,result,func 来源: https://www.cnblogs.com/gongxianjin/p/16181701.html