武汉理工大学python123实验——字符串

1.判断火车票座位 #2438

python 复制代码
###########有问题

def seat_numbers(seat):           # 判定座位是否合法
    if not(seat[:-1].isdigit()):# 万一输入是 2c1c排除
        return  False
    if 1 <= int(seat[:-1]) <= 17 and seat[-1] in 'ABCDF':
        return True
    else:
        return False


def window_or_aisle(seat):       # 判定是窗口、过道还是中间
    if seat[-1] in 'AF':
        return '窗口'
    elif seat[-1] in 'CD':
        return '过道'
    elif seat[-1] == 'B':
        return '中间'


if __name__ == '__main__':
    Seat = input().upper()
    if seat_numbers(Seat):
        print(window_or_aisle(Seat))
    else:
        print('输入错误')

2.统计单词的数量 #95

python 复制代码
a = list(input().strip().split())
print(len(a))

3.凯撒密码------加密 #117092

python 复制代码
import string

def caesar_cipher(text):
    """接收一个字符串为参数,采用字母表和数字中后面第3个字符代替当前字符的方法
    对字符串中的字母和数字进行替换,实现加密效果,返回值为加密的字符串。
    例如:2019 abc 替换为5342 def """
    lowers = string.ascii_lowercase
    #lowers是全部的小写英文字母
    tolow = lowers[3:]
    tolow+=lowers[0:3]

    uppers = string.ascii_uppercase
    toupp = uppers[3:]
    toupp+=uppers[0:3]
    #uppers是全部的大写英文字母
    digits = string.digits #digits是全部的数字字符
    todig = digits[3:]
    todig += digits[0:3]

    table = str.maketrans(lowers+uppers+digits,tolow+toupp+todig)

    ans = text.translate(table)
    return ans


if __name__ == '__main__':
    plaintext = input()
    print(caesar_cipher(plaintext))

4.汽车限行 #217427

python 复制代码
import string

def verify(car_ID): #判断车牌号car_ID是否合法
    if car_ID[0:3]!='鄂A-':
        return False
    elif len(car_ID)!=8:
        return False
    elif 'O' in car_ID or 'I' in car_ID:
        return False
    
    ls = [x for x in car_ID[3:] if x.isalpha()] 
    if len(ls)>2:
        return False
    return True
    
    
car = input()

if verify(car):
#####
    if car[-1].isdigit():
        if int(car[-1])%2==0:
            print('双号通行')
        elif int(car[-1])%2==1:
            print('单号通行')
    elif car[-2].isdigit():
        if int(car[-2])%2==0:
            print('双号通行')
        elif int(car[-2])%2==1:
            print('单号通行')
    elif car[-3].isdigit():
        if int(car[-3])%2==0:
            print('双号通行')
        elif int(car[-3])%2==1:
            print('单号通行')
        
else:
#####
    print('Data Error!')

5.缩写月份单词 # 1790

python 复制代码
a = input()

month_lst = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

low_lst = [x.lower() for x in month_lst]

b = a.lower()

if b not in low_lst:
    print('spelling mistake')
else:
    if b == 'september':
        print('Sept.')
    else:
        print(b.capitalize()[0:3]+'.')
    # index = low_lst.index(b)
    # print(month_lst[index][0:3]+'.')

6字符串移位# 71701

python 复制代码
# 需要注意s为空的情况,len(s)=0,取模运算会报错
s = input()
n = int(input())
if n==0 or s=='':
    print(s)
else:
    if n>0:
        n = n%len(s) # 取模
        print(s[len(s)-n:]+s[0:len(s)-n])
    else:
        n = -n
        n = n%len(s)
        print(s[n:]+s[0:n])
    

7.个人信息提取(字符串)#63922

python 复制代码
a = list(input().split())
print(f'姓名:{a[1]}')
print(f'班级:{a[2]}')
print(f'出生:{a[4][0:4]}年')

8. 字母查找2.0(函数)#76334

python 复制代码
########注意replace来消除字符串的指定单个字符
def f(m,n):
    #定义函数体完成题目要求功能
    for i in m:
        if i in n:
            n=n.replace(i,'',1)
        else:
            return 'NOT FOUND'
    
    return 'FOUND'
    

m=input()
if m.isalpha():    #完成该条件分支,输入字符串n判断单词m是否可以由n中的某些字符组成
    ####
    n = input()
    print(f(m,n))
else:
    print('ERROR')
相关推荐
waterHBO1 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio6 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal7 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ8 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.8 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~9 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算