100+Python挑战性编程练习系列 -- day 6

Question 18

一个网站需要用户输入用户名和密码注册。编写一个程序来检查用户输入的密码的有效性。
以下是检查密码的标准:
[a-z]之间至少有1个字母
[0-9]之间至少有1个数字
[A-Z]之间至少有1个字母
[$#@]中至少有1个字符
交易密码最小长度:6
交易密码最大长度:12
你的程序应该接受一个逗号分隔的密码序列,并将根据上述标准检查它们。将打印符合条件的密码,每个密码之间用逗号分隔。
示例
如果将以下密码作为程序的输入:
ABd1234@1,a F1#,2w3E *,2We3345
然后,程序的输出应该是:
ABd1234@1

方法1:

def is_low(x):                  # Returns True  if the string has a lowercase
    for i in x:
        if 'a'<=i and i<='z':
            return True
    return  False

def is_up(x):                   # Returns True  if the string has a uppercase
    for i in x:
        if 'A'<= i and i<='Z':
            return True
    return  False

def is_num(x):                  # Returns True  if the string has a numeric digit
    for i in x:
        if '0'<=i and i<='9':
            return True
    return  False

def is_other(x):                # Returns True if the string has any "$#@"
    for i in x:
        if i=='$' or i=='#' or i=='@':
            return True
    return False

s = input().split(',')
lst = []

for i in s:
    length = len(i)
    if 6 <= length and length <= 12 and is_low(i) and is_up(i) and is_num(i) and is_other(i):   #Checks if all the requirments are fulfilled
        lst.append(i)

print(",".join(lst))

方法2:

import  re

s = input().split(',')
lst = []

for i in s:
    cnt = 0
    cnt+=(6<=len(i) and len(i)<=12)
    cnt+=bool(re.search("[a-z]",i))      # here re module includes a function re.search() which returns the object information
    cnt+=bool(re.search("[A-Z]",i))      # of where the pattern string i is matched with any of the [a-z]/[A-z]/[0=9]/[@#$] characters
    cnt+=bool(re.search("[0-9]",i))      # if not a single match found then returns NONE which converts to False in boolean
    cnt+=bool(re.search("[@#$]",i))      # expression otherwise True if found any.
    if cnt == 5:
        lst.append(i)

print(",".join(lst))

方法3:

import re
a = input('Enter passwords: ').split(',')
pass_pattern = re.compile(r"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$#@]).{6,12}$")
for i in a:
    if pass_pattern.fullmatch(i):
        print(i)

Question 19

您需要编写一个程序来按升序对(name,age,score)元组进行排序,其中name是字符串,age和score是数字。元组由控制台输入。排序条件为:
1:根据名称排序
2:然后根据年龄排序
3:然后按分数排序
优先级是姓名 〉年龄 〉分数。
如果将以下元组作为程序的输入:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
然后,程序的输出应该是:
[(‘John’, ‘20’, ‘90’), (‘Jony’, ‘17’, ‘91’), (‘Jony’, ‘17’, ‘93’), (‘Json’, ‘21’, ‘85’), (‘Tom’, ‘19’, ‘80’)]

方法1:

lst = []
while True:
    s = input().split(',')
    if not s[0]:                          # breaks for blank input
        break
    lst.append(tuple(s))

lst.sort(key= lambda x:(x[0],int(x[1]),int(x[2])))  # here key is defined by lambda and the data is sorted by element priority 0>1>2 in accending order
print(lst)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值