题目 18
一个网站需要用户输入用户名和密码注册。编写一个程序来检查用户输入的密码的有效性。 以下是检查密码的标准:
- [a-z] 之间至少有 1 个字母
- [0-9] 之间至少有 1 个数字
- [A-Z] 之间至少有 1 个字母
- [$#@] 中至少有 1 个字符
- 交易密码最小长度:6
- 交易密码最大长度:12
你的程序应该接受一个逗号分隔的密码序列,并将根据上述标准检查它们。将打印符合条件的密码,每个密码之间用逗号分隔。
示例 如果将以下密码作为程序的输入:
ABd1234@1,a F1#,2w3E *,2We3345然后,程序的输出应该是:ABd1234@1
方案一
def is_low(x):
for i in x:
if 'a' <= i and i <= 'z':
return True
return False
def is_up(x):
for i in x:
if 'A' <= i and i <= 'Z':
return True
return False
def is_num(x):
for i in x:
if '0' <= i and i <= '9':
return True
return False
def is_other(x):
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):
lst.append(i)
print(",".join(lst))
方案二
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))
cnt += bool(re.search("[A-Z]", i))
cnt += bool(re.search("[0-9]", i))
cnt += bool(re.search("[@#$]", i))
if cnt == 5:
lst.append(i)
print(",".join(lst))
方案三
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)
题目 19
您需要编写一个程序来按升序对(name,age,score)元组进行排序,其中 name 是字符串,age 和 score 是数字。元组由控制台输入。排序条件为:
- 根据名称排序
- 然后根据年龄排序
- 然后按分数排序
优先级是姓名 > 年龄 > 分数。
示例 如果将以下元组作为程序的输入:
Tom,19,80John,20,90Jony,17,91Jony,17,93Json,21,85然后,程序的输出应该是:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
方案一
lst = []
while True:
s = input().split(',')
if not s[0]:
break
lst.append(tuple(s))
lst.sort(key=lambda x: (x[0], int(x[1]), int(x[2])))
print(lst)
