checkio题目

我们现在这儿: 丢人只解决了3道题目
checkio题目_第1张图片

  • 就不按照原来的顺序了 有些题目暂时解决不了,下面按照已经解决的顺序更新答案

  • Non-unique Elements
    题目要求:
    checkio题目_第2张图片
    解题思路:列表有一个count()方法 可以统计每个元素的个数,在加上列表推导式,所以答案呼之欲出咯
    废话少说 上代码

def checkio(data: list) -> list:
    # Your code here
    # It's main function. Don't remove this function
    # It's used for auto-testing and must return a result for check.
    # 一行就搞定 利用列表推导式生成它需要的新列表 用count函数统计元素个数
    return [i for i in data if data.count(i) > 1]	


if __name__ == "__main__":
    # These "asserts" using only for self-checking and not necessary for auto-testing
    assert list(checkio([1, 2, 3, 1, 3])) == [1, 3, 1, 3], "1st example"
    assert list(checkio([1, 2, 3, 4, 5])) == [], "2nd example"
    assert list(checkio([5, 5, 5, 5, 5])) == [5, 5, 5, 5, 5], "3rd example"
    assert list(checkio([10, 9, 10, 10, 9, 8])) == [10, 9, 10, 10, 9], "4th example"
    print("It is all good. Let's check it now")
  • All the Same
  • 题目要求:
    checkio题目_第3张图片
  • 答案
from typing import List, Any


def all_the_same(elements: List[Any]) -> bool:
    # your code here
    # 此处用到了三目运算符和set来去重
    return True if len(set(elements)) <= 1 else False


if __name__ == '__main__':
    print("Example:")
    print(all_the_same([1, 1, 1]))
    
    # These "asserts" are used for self-checking and not for an auto-testing
    assert all_the_same([1, 1, 1]) == True
    assert all_the_same([1, 2, 1]) == False
    assert all_the_same(['a', 'a', 'a']) == True
    assert all_the_same([]) == True
    assert all_the_same([1]) == True
    print("Coding complete? Click 'Check' to earn cool rewards!")
  • House Password
  • 题目要求
    checkio题目_第4张图片
import re
def checkio(data: str) -> bool:
    module = re.compile("(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{10,}")
    if 0 < len(data) <= 64:
        return bool(module.match(data))
 


#Some hints
#Just check all conditions


if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio('A1213pokl') == False, "1st example"
    assert checkio('bAse730onE4') == True, "2nd example"
    assert checkio('asasasasasasasaas') == False, "3rd example"
    assert checkio('QWERTYqwerty') == False, "4th example"
    assert checkio('123456123456') == False, "5th example"
    assert checkio('QwErTy911poqqqq') == True, "6th example"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

你可能感兴趣的:(Python学习)