上机编程认证02---验证密码

2022/4/23 11:42:31

本文主要是介绍上机编程认证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---验证密码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程