Python中关于字符串反码的问题1

描述

字符串反码的定义为:字符串所包含字符的反码组成的字符串。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

字符反码的定义为:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

(1) 对于小写英文字符,它的反码也是一个小写英文字符,且该字符与'a'的距离等于其反码与'z'的距离;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

(2) 对于大写英文字符,它的反码也是一个大写英文字符,且该字符与'A'的距离等于其反码与'Z'的距离;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬

两个字符距离指其对应unicode编码之差。

下面是第一次尝试写的代码:

s=input()
i=0
j=0
len=len(s)

str0=[0]*len
        
while(i<len):
    if(s[i].isalpha()):
            if(65<=ord(s[i]) and ord(s[i])<=77):
                temp1=ord(s[i])
                temp2=ord('Z')-(temp1-ord('A'))
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            elif(78<=ord(s[i]) and ord(s[i])<=90):
                temp1=ord(s[i])
                temp2=ord('A')+(ord('Z')-temp1)
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            elif(97<=ord(s[i]) and ord(s[i])<=109):
                temp1=ord(s[i])
                temp2=ord('z')-(temp1-ord('a'))
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            elif(110<=ord(s[i]) and ord(s[i])<=122):
                temp1=ord(s[i])
                temp2=ord('a')+(ord('z')-temp1)
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            else:
                i+=1
    else:
        str0[i]=s[i] 
        i+=1
for test in str0:
    print(test,end='')

代码冗杂,且不能处理中文字符

下面是改进的代码,引用自fishc的isdkz大佬:

问题出在对于英文字符的反码处理上。代码中使用了replace()方法来替换字符,然后再将替换后的字符串转换为整数,最后再转换为字符。这样的处理方式是不正确的。

正确的处理方式是使用ord()函数获取字符的ASCII码,然后根据ASCII码进行反码处理,最后再将反码转换为字符。

以下是修正后的代码:

 

s = input()
str0 = []

for ch in s:
    if ch.isalpha():
        if 'A' <= ch <= 'M':
            temp1 = ord(ch)
            temp2 = ord('Z') - (temp1 - ord('A'))
            str0.append(chr(temp2))
        elif 'N' <= ch <= 'Z':
            temp1 = ord(ch)
            temp2 = ord('A') + (ord('Z') - temp1)
            str0.append(chr(temp2))
        elif 'a' <= ch <= 'm':
            temp1 = ord(ch)
            temp2 = ord('z') - (temp1 - ord('a'))
            str0.append(chr(temp2))
        elif 'n' <= ch <= 'z':
            temp1 = ord(ch)
            temp2 = ord('a') + (ord('z') - temp1)
            str0.append(chr(temp2))
        else:
            str0.append(ch)
    else:
        str0.append(ch)

result = ''.join(str0)
print(result)


这样修改后,输入 "学好Python,用Coding建设祖国",输出结果为 "学好Kbgslm,用Xlwrmt建设祖国",符合预期的结果。

分析:

① for ch in s 替代原while循环,省去了 i<len 的限制

②根据ASCII字符码进行运算

③声明空列表,再用append赋值更简洁

④打印时用join()方法更简洁

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

join()方法语法:

str.join(sequence)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值