记录一个未完

一个代码实现:

一些基础知识不过关,如下:

1. 判断大小写字母:isuppper()islower()
2. 大小写互换:uppper()lower()
3. 字母转为数字:ord()
4. 数字转回字母:chr()
5. 列表转为字符串:''.join(l),其中l为一个列表
6. 字符串转为列表:list(s),其中s为一个字符串
7. 字符串中替换:s.replace(old,new)


下列代码实现的功能为:(i) 将大小写字母互换; (ii) 将字母加1,得到下一个字母。

def One(ll: str):
    temp = list(ll)
    length = len(temp)
    for i in range(length):
        if temp[i].islower():
            temp[i] = temp[i].upper()
        elif temp[i].isupper():
            temp[i] = temp[i].lower()
    # list 转为 str
    return ''.join(temp)


def Two(ll: str):
    temp = list(ll)
    length = len(temp)
    for i in range(length):
        if temp[i] == 'z':
            temp[i] = 'a'
        elif temp[i] == 'Z':
            temp[i] = 'A'
        else:
            temp[i] = chr(ord(temp[i]) + 1)
            #ord(): ASCII  chr(): 转回字符

        # list 转为 str
    return ''.join(temp)


n, q = map(int, input().split())
s = input()
for i in range(q):
    opt, l, r = map(int, input().split())
    if opt == 1:
        s = s.replace(s[(l - 1):r], One(s[(l - 1):r]))
        print(s)
    if opt == 2:
        s = s.replace(s[(l - 1):r], Two(s[(l - 1):r]))
        print(s)

由于python中字符串是不可直接修改的,上文中处理字符替换使用了先转换为列表再换回字符串,这样也可以较好的处理有重复字符的情况

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值