Python-programming-exercises问题18

  • 问题:
    网站要求用户输入用户名和密码进行注册。编写程序以检查用户输入的密码是否有效。
    以下是检查密码的标准:
  • [az]之间至少1个字母
  • [0-9]之间至少1个数字
  • [AZ]之间至少1个字母
  • [$#@]中的至少1个字符
    4.最小交易密码长度:6
    5.交易密码的最大长度:12
    您的程序应接受逗号分隔的密码序列,并将根据上述条件进行检查。符合条件的密码将被打印,每个密码之间用逗号分隔。

    如果输入以下密码作为程序输入:
    ABd1234 @ 1,a F1#,2w3E *,2We3345
    然后,程序的输出应为:
    ABd1234 @ 1
  • 我的代码:
s = list(input().split(','))
a = []
for i in s:
    k,l,m,n = 0,0,0,0
    i = [str(x) for x in i]
    if 6<=len(i)<=12:
        for j in i:
            if j.isnumeric():
                k = k+1
            elif j.islower():  #小写
                l = l+1
            elif j.isupper():  #大写
                m = m+1
            elif j =='$'or j=='#' or j=='@':
                n = n+1
        if k!=0 and l!= 0 and m!=0 and n!=0:
            a.append(''.join(i))
            
print(''.join(a))
  • Solution:
import re
value = []
items=[x for x in input().split(',')]
for p in items:
    if len(p)<6 or len(p)>12:
        continue
    else:
        pass
    if not re.search("[a-z]",p):
        continue
    elif not re.search("[0-9]",p):
        continue
    elif not re.search("[A-Z]",p):
        continue
    elif not re.search("[$#@]",p):
        continue
    elif re.search("\s",p):
        continue
    else:
        pass
    value.append(p)
print (",".join(value))
  • 分析:
    Solution中使用了正则表达式re模块中re.search()函数:
    具体用法:
re.search(pattern, string, flags=0)

函数参数说明:
来源:菜鸟教程
题目来源:https://github.com/zhiwehu/Python-programming-exercises/blob/master/100+%20Python%20challenging%20programming%20exercises.txt

“近年来程序设计领域最好的一本书。”——Larry O’Brien, Software Development Times, July 29, 2015 Using a simple computational task (term frequency) to illustrate different programming styles, Exercises in Programming Style helps readers understand the various ways of writing progr ams and designing systems. It is designed to be used in conjunction with code provided on an online repository. The book complements and explains the raw code in a way that is accessible to anyone who regularly practices the art of programming. The book can also be used in advanced programming courses in computer science and software engineering programs. The book contains 33 different styles for writing the term frequency task. The styles are grouped into nine categories: historical, basic, function composition, objects and object interactions, reflection and metaprogramming, adversity, data-centric, concurrency, and interactivity. The author verbalizes the constraints in each style and explains the example programs. Each chapter first presents the constraints of the style, next shows an example program, and then gives a detailed explanation of the code. Most chapters also have sections focusing on the use of the style in systems design as well as sections describing the historical context in which the programming style emerged. Cristina Videira Lopes,加州大学欧文分校信息学教授,致力于大规模数据和系统的软件工程研究。她是施乐帕洛阿尔托研究中心的创始成员,还研发并维护着一个搜索引擎,为基于OpenSimulator的虚拟世界提供帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值