题解 | 字符串变形

字符串变形

https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @param n int整型 
# @return string字符串
#

"""
关键点:
 ord 的使用 判断字符的ascii 码 大小写字母
 字符串数组的逆序: s=s[::-1]


"""
class Solution:
    def trans(self , s: str, n: int) -> str:
        # write code here
        line=""
        for ch in s:
            ret_ch=self.func(ch)
            line+=str(ret_ch)
        
        #分割
        st_list=list(line.split(" "))
        st_list=st_list[::-1]
        res=list()
        return " ".join(st_list)  # 用空格隔开
            
    def func(self,ch):
        if ord("A")<=ord(ch)<=ord("Z"):
            return ch.lower()
        if ord("a")<= ord(ch)<=ord("z"):
            return ch.upper()
        else:
            return ch
        

全部评论

相关推荐

Debug_EVE:简历不要做成左右两页的,尽量做成上下一页
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务