利用python的split函数即可,按照空格分开后倒着遍历,然后遍历的同时大小写转换即可。关键可能是要记得python的split函数,大小写转换函数upper()和lower()就行了。
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @param n int整型
# @return string字符串
#
class Solution:
def trans(self , s: str, n: int) -> str:
# write code here
new = ''
s = s.split(' ')
for i in range(len(s)):
tmp = s[len(s)-i-1]
for j in range(len(tmp)):
if tmp[j] >= 'A' and tmp[j] <= 'Z':
new = new+tmp[j].lower()
elif tmp[j] >= 'a' and tmp[j] <= 'z':
new = new+tmp[j].upper()
new = new+' '
return new[:-1]
s = "This is a sample"
print(Solution().trans(s , 16))