酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

7-1 句子反转 分数 30 作者 陈宇璟 单位 浙江大学 给定一行句子,每个词之间用空格隔开,要么是全小写英文单词,要么是全大写英文单词,要么是自然数。 要求将这些单词倒序输出。而且对于每个单词,如果是小写词,应当转为大写;如果是大写词,应当转为小写;如果是自然数,应该倒转输出。 举一个例子: we choose to go 2 the 123 moon 程序应当输出: moon 321 the 2 go to choose we 输入格式 仅一行,即需要反转的句子。 输出格式 仅一行,表示程序对句子的处理结果。 说明/提示 样例解释 首先应当按单词逆序,即: moon 123 the 2 go to choose we 小写变大写、大写变小写、倒转自然数之后,得到最终结果: moon 321 the 2 go to choose we 数据规模与约定 对于 100% 的数据,句子中包含的单词数量不超过 1000,每个单词长度不超过 6。 输入样例: we choose to go 2 the 123 moon 输出样例: moon 321 the 2 go to

以下是使用 Python 实现的代码: ```python def reverse_sentence(sentence): words = sentence.split() reversed_words = [] for word in words[::-1]: if word.islower(): reversed_words.append(word.upper()) elif word.isupper(): reversed_words.append(word.lower()) elif word.isdigit(): reversed_words.append(word[::-1]) else: reversed_words.append(word) return " ".join(reversed_words) sentence = input() print(reverse_sentence(sentence)) ``` [2024-11-11 16:44:49 | AI写代码神器 | 131点数解答]

相关提问