当前位置:首页 >> 编程语言 >> 【Python CheckiO 题解】All the Same,诺亚舟np1100

【Python CheckiO 题解】All the Same,诺亚舟np1100

0evadmin 编程语言 1
文件名:【Python CheckiO 题解】All the Same,诺亚舟np1100 【Python CheckiO 题解】All the Same

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


题目描述

【All the Same】:检查给定的列表,判断是否其中所有的元素都相等

【链接】:https://py.checkio.org/mission/all-the-same/

【输入】:列表(List)

【输出】:布尔值(Bool),True 或者 False

【范例】:

all_the_same([1, 1, 1]) == Trueall_the_same([1, 2, 1]) == Falseall_the_same(['a', 'a', 'a']) == Trueall_the_same([]) == True 解题思路

利用 set() 函数删除重复数据,如果其长度小于等于1,返回 True,否则返回 False。

代码实现 from typing import List, Anydef all_the_same(elements: List[Any]) -> bool:if len(set(elements)) <= 1:return Trueelse:return Falseif __name__ == '__main__':print("Example:")print(all_the_same([1, 1, 1]))# These "asserts" are used for self-checking and not for an auto-testingassert all_the_same([1, 1, 1]) == Trueassert all_the_same([1, 2, 1]) == Falseassert all_the_same(['a', 'a', 'a']) == Trueassert all_the_same([]) == Trueassert all_the_same([1]) == Trueprint("Coding complete? Click 'Check' to earn cool rewards!") 大神解答 大神解答 NO.1 def all_the_same(elements):return elements[1:] == elements[:-1] 大神解答 NO.2 def all_the_same(elements):return len(elements) < 1 or len(elements) == elements.count(elementse[0])
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接