当前位置:首页 >> 编程语言 >> 【Python CheckiO 题解】Secret Message,音箱评测

【Python CheckiO 题解】Secret Message,音箱评测

0evadmin 编程语言 1
文件名:【Python CheckiO 题解】Secret Message,音箱评测 【Python CheckiO 题解】Secret Message

CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。

CheckiO 官网:https://checkio.org/

我的 CheckiO 主页:https://py.checkio.org/user/TRHX/

CheckiO 题解系列专栏:https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有题解源代码:https://github.com/TRHX/Python-CheckiO-Exercise


题目描述

【Secret Message】:找到一个字符串中所有的大写字母,返回结果为字符串形式,且大写字母要按照原字符串中的顺序排列。

【链接】:https://py.checkio.org/mission/secret-message/

【输入】:一段文本(unicode)

【输出】:所有大写字母组成的字符串或者空字符串

【前提】: 0 < len(text) ≤ 1000

【范例】:

find_message("How are you? Eh, ok. Low or Lower? Ohhh.") == "HELLO"find_message("hello world!") == "" 解题思路

直接利用 re 模块的 findall 方法,提取原字符串中的所有大写字母,然后再用 join() 方法将所有大写字母转换成字符串即可

代码实现 import redef find_message(text: str) -> str:"""Find a secret message"""text = re.findall('[A-Z]', text)text = ''.join(text)return textif __name__ == '__main__':print('Example:')print(find_message("How are you? Eh, ok. Low or Lower? Ohhh."))#These "asserts" using only for self-checking and not necessary for auto-testingassert find_message("How are you? Eh, ok. Low or Lower? Ohhh.") == "HELLO", "hello"assert find_message("hello world!") == "", "Nothing"assert find_message("HELLO WORLD!!!") == "HELLOWORLD", "Capitals"print("Coding complete? Click 'Check' to review your tests and earn cool rewards!") 大神解答 大神解答 NO.1 def find_message(text: str) -> str:"""Find a secret message"""return "".join(s for s in text if s.isupper()== True) 大神解答 NO.2 def find_message(text: str) -> str:return ''.join(list(filter(lambda x: x.isupper(), text))) 大神解答 NO.3 def find_message(text):"""Find a secret message"""message = ''for i in text:if i.isupper(): message +=ireturn message
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接