Leetcode:420. 强密码检验器 Python解题,re + list+切片+set+循环判断

话不多说,直接上代码,使用re正则表达式,循环判断,list,set,用到一些基本知识即可:

'''密码要求如下:
            密码长度不小于6,不大于20
            至少包含一个数字,一个小写字母,一个大写字母
            单个字符不允许连续重复三次以上'''
import re
# s = '232333dAsssww.,.,.$#'

def strongPasswordChecker(s):
    if not s.strip() or re.search('\s+',s):
        print('密码不允许为空或者包含空格!')
    elif len(s) >= 6 and len(s) <= 20:
        nums = 0
        if re.search('[a-z]+',s) and re.search('[A-Z]+',s) and re.search('[0-9]+',s):
            count = []
            for i in s:
                count.append(s.count(i))
            if max(count) < 3:
                return 0
            else:
                for i in range(len(s)):
                    if len(s[i:]) >= 3:
                        if set(s[i:i+3]) == set(s[i]):
                            nums+=1
                if nums > 0:
                    print('密码不合格,单个字符不允许连续重复三次!')
                return nums
        elif not re.search('[a-z]+',s):
            nums +=1
            print('密码不合格,密码中需至少包含一位小写字母!')
        if not re.search('[A-Z]+',s):
            nums +=1
            print('密码不合格,密码中需至少包含一位大写字母!')
        if not re.search('[0-9]+',s):
            nums +=1
            print('密码不合格,密码中需至少包含一位数字!')
        return nums
    elif len(s) < 6:
        print('密码不合格,密码长度不少于6位!')
        return 6 - len(s)
    elif len(s) > 20:
        print('密码不合格,密码长度不大于20位!')
        return len(s) - 20

i = 0
while i < 3:
    s = input('请输入您的密码:')
    print('您设置的密码长度为:{}'.format(len(s)))
    ss = strongPasswordChecker(s)
    if ss == 0:
        print('密码等级高,符合要求!')
        break
    else:
        print('密码不符合要求,密码合规需要修改的次数为:{}'.format(ss))
        i+=1

主要在于思路和逻辑判断。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值