02_checkio_House_Passworld

Stephan and Sophia forget about security and use simple passwords for everything. Help Nikola develop a password security check module. The password will be considered strong enough if its length is greater than or equal to 10 symbols, it has at least one digit, as well as containing one uppercase letter and one lowercase letter in it. The password contains only ASCII latin letters or digits.

Input: A password as a string.

Output: Is the password safe or not as a boolean or any data type that can be converted and processed as a boolean. In the results you will see the converted results.

Example:

checkio('A1213pokl') == False
checkio('bAse730onE') == True
checkio('asasasasasasasaas') == False
checkio('QWERTYqwerty') == False
checkio('123456123456') == False
checkio('QwErTy911poqqqq') == True
def checkio(data):

    #replace this for solution
    return True or False

#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!")

代码:

def checkio(data):
    if len(data)>9:
        if any(i.isupper() for i in data) and any(i.islower() for i in data) and any(i.isdigit() for i in data):
            return True
        else:
            return False
    else:
        return False

其它人的代码:

checkio = lambda s: not(
        len(s) < 10
        or s.isdigit()
        or s.isalpha()
        or s.islower()
        or s.isupper()
    ) 

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    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"
import re

DIGIT_RE = re.compile('\d')
UPPER_CASE_RE = re.compile('[A-Z]')
LOWER_CASE_RE = re.compile('[a-z]')

def checkio(data):
    """
    Return True if password strong and False if not
    
    A password is strong if it contains at least 10 symbols,
    and one digit, one upper case and one lower case letter.
    """
    if len(data) < 10:
        return False
    
    if not DIGIT_RE.search(data):
        return False

    if not UPPER_CASE_RE.search(data):
        return False

    if not LOWER_CASE_RE.search(data):
        return False
        
    return True

if __name__ == '__main__':
    assert checkio('A1213pokl')==False, 'First'
    assert checkio('bAse730onE4')==True, 'Second'
    assert checkio('asasasasasasasaas')==False, 'Third'
    assert checkio('QWERTYqwerty')==False, 'Fourth'
    assert checkio('123456123456')==False, 'Fifth'
    assert checkio('QwErTy911poqqqq')==True, 'Sixth'
    print('All ok')
import re
def checkio(data):
    return True if re.search("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$", data) and len(data) >= 10 else False


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"

def checkio(data):
    import re
    if len(data)<10:
        return False
    if not re.search('[0-9]', data):
        return False
    if not re.search('[a-z]', data):
        return False
    if not re.search('[A-Z]', data):
        return False
    return True

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    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"
def checkio(data):
    if len(data) < 10:
        return False
    if data.upper() == data:
        return False
    if data.lower() == data:
        return False
    return any(c.isdigit() for c in data)

#Some hints
#Just check all conditions

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    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"

any() 函数的用法,以及与all()函数的区别,原文地址:https://www.cnblogs.com/nulige/p/6128816.html

any()与all()函数的区别:

  any是任意,而all是全部。 

 

版本:该函数适用于2.5以上版本,兼容python3.x版本。

 

any(...)

    any(iterable) -> bool

    

    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.

 

any(iterable)说明:参数iterable:可迭代对象;

如果当iterable所有的值都是0、''或False时,那么结果为False,如果所有元素中有一个值非0、''或False,那么结果就为True

函数等价于:

复制代码
1 def any(iterable):
2 
3    for element in iterable:
4 
5        if  element:
6 
7            return False
8 
9    return True
复制代码

 

示例:

复制代码
 1 >>> any(['a', 'b', 'c', 'd'])  #列表list,元素都不为空或0
 2 True
 3  
 4 >>> any(['a', 'b', '', 'd'])  #列表list,存在一个为空的元素
 5 True
 6  
 7 >>> any([0, '', False])  #列表list,元素全为0,'',false
 8 False
 9  
10 >>> any(('a', 'b', 'c', 'd'))  #元组tuple,元素都不为空或0
11 True
12  
13 >>> any(('a', 'b', '', 'd'))  #元组tuple,存在一个为空的元素
14 True
15  
16 >>> any((0, '', False))  #元组tuple,元素全为0,'',false
17 False
18   
19 >>> any([]) # 空列表
20 False
21  
22 >>> any(()) # 空元组
23 False
复制代码

 

all(...)

    all(iterable) -> bool

    

    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.

 

如果iterable的所有元素不为0、''、False或者iterable为空,all(iterable)返回True,否则返回False;函数等价于:

复制代码
1 def all(iterable):
2 
3     for element in iterable:
4         if not element:
5             return False
6     return True
复制代码

 

示例:

复制代码
 1 >>> all(['a', 'b', 'c', 'd'])  #列表list,元素都不为空或0
 2 True
 3 >>> all(['a', 'b', '', 'd'])  #列表list,存在一个为空的元素
 4 False
 5 >>> all([0, 1,2, 3])  #列表list,存在一个为0的元素
 6 False
 7    
 8 >>> all(('a', 'b', 'c', 'd'))  #元组tuple,元素都不为空或0
 9 True
10 >>> all(('a', 'b', '', 'd'))  #元组tuple,存在一个为空的元素
11 False
12 >>> all((0, 1,2, 3))  #元组tuple,存在一个为0的元素
13 False
14    
15 >>> all([]) # 空列表
16 True
17 >>> all(()) # 空元组
18 True
复制代码

注意:空元组、空列表返回值为True,这里要特别注意。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值