武汉理工大学python123实验---列表

1.模拟砍价

####随机数种子的设定
a,b = input().split(',')

import random

random.seed(int(b))

a = int(a)
b= a
cnt = 0
while a>0:
    a = a- random.randint(0,int(b/10))
    cnt+=1
print(cnt)

2.素数求和

def isp(n):
    if n==2:
        return True
    if n<2:
        return False
    if n%2==0:
        return False
        
    for i in range(3,int(n**0.5)+1,2):
        if n%i==0:
            return False
    return True
    
a = int(input())
# 下面会超时
# ls = [i for i in range(a,1,-1) if isp(i)]
ls=[]
cnt = 0
for i in range(a,1,-1):
    if cnt>=10:
        break
    if isp(i):
        ls.append(i)
        cnt+=1

ls = ls[0:10]
ls.reverse()
print(ls[0],end="")
for i in ls[1:]:
    
    print('+'+str(i),end="")
print('='+str(sum(ls)))

3.个人数据脱敏

#替换为固定符号,不一定要用replace~

n = int(input())
if n<=0:
    print('ERROR')
else:
    ls=[]
    for i in range(n):
        a=list(input().split())
        # 将a[1]的第5-11位改为*号
        a[0]=a[0][:4]+'*'*7+a[0][11:]
        # 将a[2]的第2位改为*号
        a[1]=a[1][:1]+'*'+a[1][2:]
        # 将a[3]的第4-7位改为*号
        a[2]=a[2][:3]+'*'*4+a[2][7:]
        ls.append(a)
    print(ls)
    

4.文本分析(1)——统计文件中的字符   #117566

import string

def read_file(file):
    """接收文件名为参数,读取文件中的数据到字符串中,返回这个字符串"""
    with open(file, 'r', encoding='utf-8') as f:
        return f.read()

def classify_char(txt):
    """接收字符串为参数,依序返回大写字母、小写字母、数字、空格、和其他字符数量"""
    upper, lower, digit, space, other = 0, 0, 0, 0, 0
    txt = txt.strip()
    for i in txt:
        if i.isdigit():
            digit+=1
        elif i.islower():
            lower+=1
        elif i.isupper():
            upper+=1
        elif i==' ':
            space+=1
        else:
            other+=1
    return upper,lower,digit,space,other
            

    return upper, lower, digit, space, other

if __name__ == '__main__':
    filename = input()              # 读入文件名
    text = read_file(filename)      # 调用函数读文件中的内容为字符串
    classify = classify_char(text)  # 调用函数分类统计字符数量
    print('大写字母{}个,小写字母{}个,数字{}个,空格{}个,其他{}个'.format(*classify))
        elif i==' ':


5.分解质因数        #2642

a = int(input())
b = a
ls=[]
for i in range(2,a):
    if a==1:
        break
    while a%i==0:
        ls.append(i)
        a=a/i
print(ls)

6.摩尔斯电码转换    #18993

#字母转ascii码:ord('A')
ls = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.", "---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

a = input()


s  = a

for i in s:
    if i.isalpha():
        print(ls[ord(i.lower())-ord('a')],end='')
    else: 
        print(i,end="")


    

7.插入位置   #72375

# 最后的特判,新元素比列表中的都大
a = list(map(int,list(input().split())))
b = int(input())

if b in a:
    print('Fail')
    print(a)
else:
    for i in range(0,len(a)):
        if a[i]>b:
            print(i)
            a.insert(i,b)
            print(a)
            break
    else:
        print(len(a))
        a.append(b)
        print(a)

8.列表删除数据         #72957

# 列表的pop和index

a = list(map(int,list(input().split())))
b = int(input())

if b not in a:
    print('NOT FOUND')
else:
    while b in a:
        a.pop(a.index(b))
    print(a)

  • 23
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值