几个简单的例子

输入名字、邮箱以及学号。
问题:
1.求数字数列的平均数,输出结果保留两位小数。
2.分析字符数列中大写字母、小写字母、数字、以及其他字符的个数,以数列形式输出。
3.比较两个数字数列,求交集。注:交集中无重复数字。
4.找出数列的第二大的数字。注:如果数列只有一个数字,则输出一个,如果为空或者有非数字字符输出-99555。
5.写一个密码要求,需要字符大于等于10,包含大写字母小写字母,至少3个数字,至少2个其他字符,否则输出false。

#把名字和缺的东西补全
def studentname(myName,myEmail,myBBUsername):
    myName='xxxxx'
    myEmail='xxx@xxx'
    myBBUsername='xxxxxx'
    return myName,myEmail,myBBUsername

#第一个问题
def calculateAverage(list_1):
    n=len(list_1)
    sum_1=sum(list_1)
    mean_1=sum_1/n
    return '%.2f' % mean_1

#第二个问题
def countCharacters(string_1):
    total_nu=[]
    nu_uppercase=0
    nu_lowercase=0
    nu_digits=0
    nu_anyother=0
    for i in string_1:
        if i.isupper():
            nu_uppercase+=1
        elif i.islower():
            nu_lowercase+=1
        elif i.isdecimal():
            nu_digits+=1
        else:
            nu_anyother+=1
    total_nu.append(nu_uppercase+nu_lowercase)
    total_nu.append(nu_uppercase)
    total_nu.append(nu_lowercase)
    total_nu.append(nu_digits)
    total_nu.append(nu_anyother)
    return total_nu

#第三个问题
def excludeItems(list_1,list_2):
    list_3=[]
    for i in list_1:
        for j in list_2:
            if i==j:
                list_3.append(i)
    res=list(set(list_3))
    return res 
 
#第四个问题
def secondLargest(list_4):
    n=len(list_4)
    for i in list_4:
        if isinstance(i,int):
            continue
        else:
            return -99555
    if n==0:
        return -99555
    elif n==1:
        return list_4[0]
    elif n==2:
        return list_4[1]
    elif n>2:
        new_list_4=list(set(list_4))
        n2=len(new_list_4)
        if n2==1:
            return new_list_4[0]
        elif n2==2:
            return new_list_4[0]
        elif n2>2:
            new_list_4.sort()
            return new_list_4[-2]

#第五个问题
def isValidPassword(string_2):
    n=len(string_2)
    m=countCharacters(string_2) 
    if n<10:
        return False
    elif m[1]<1 or m[2]<1:
        return False
    elif m[3]<3:
        return False
    elif m[4]<2:
        return False
    else:
        return True
    
 

检查验证:

from solution import *
i=[]
j=[]
k=[]
print(studentname(i,j,k))
#第一题检查
a=[2,6,8,3,4,6]
print(calculateAverage(a))

#第二题检查
b='Hell0 WorlD!!!4567'
print(countCharacters(b))

#第三题检查
list1=[1,2,3,4,2,1]
list2=[2,4,4,2] 
list11=[1,2,3,'a',4,'b',2,1]
list22=[2,4,4,'a','c',2,'b',5] 
list111=[2,'a',2,'cat',-1] 
list222=[2,'A','cat',-1,'cat']
print(excludeItems(list1,list2))
print(excludeItems(list11,list22))
print(excludeItems(list111,list222))

#第四题检查
c=[1,2,3,4,5,6]
c1=[6,8,3,4,6]
c2=[-11, -3, -15, -9, -16]
c3=[53, 23]
c4=[13]
c5=[12, 'not number', 23]
c6=[]
print(secondLargest(c))
print(secondLargest(c1))
print(secondLargest(c2))
print(secondLargest(c3))
print(secondLargest(c4))
print(secondLargest(c5))
print(secondLargest(c6))

#第五题检查
d='Dxq111_44%@@'
d1='Dxq11##11'
d2='1111sssEh'
print(isValidPassword(d))
print(isValidPassword(d1))
print(isValidPassword(d2))

检验答案:

('xxxxx', 'xxx@xxx', 'xxxxxx')
4.83
[9, 3, 6, 5, 4]
[2, 4]
['a', 2, 4, 'b']
[2, -1, 'cat']
5
6
-9
23
13
-99555
-99555
True
False
False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值