【Python CheckiO 题解】House Password
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
題目描述
【House Password】:斯蒂芬和索菲亞對于一切都使用簡單的密碼,忘記了安全性。請你幫助尼古拉開發一個密碼安全檢查模塊。如果密碼的長度大于或等于10個字符,且其中至少有一個數字、一個大寫字母和一個小寫字母,該密碼將被視為足夠強大。密碼只包含ASCII拉丁字母或數字。
【鏈接】:https://py.checkio.org/mission/house-password/
【輸入】:密碼(字符串類型)
【輸出】:密碼的安全與否(True 或 False)
【前提】:re.match("[a-zA-Z0-9]+", password) ;0 < len(password) ≤ 64
【范例】:
checkio('A1213pokl') == False checkio('bAse730onE') == True checkio('asasasasasasasaas') == False checkio('QWERTYqwerty') == False checkio('123456123456') == False checkio('QwErTy911poqqqq') == True解題思路
利用 Python 的 re 庫,用 search() 方法掃描整個字符串,如果包含一個數字、一個大寫字母和一個小寫字母并且整個字符串長度大于或等于10個字符,則輸出 True,否則輸出 False
代碼實現
import redef checkio(data: str) -> bool:if re.search(r'[0-9]', data) and re.search(r'[a-z]', data) and re.search(r'[A-Z]', data) and len(data) >= 10:return Trueelse:return False# Some hints # Just check all conditionsif __name__ == '__main__':# These "asserts" using only for self-checking and not necessary for auto-testingassert 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!")大神解答
大神解答 NO.1
import redef checkio(data):return True if re.search("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$", data) and len(data) >= 10 else False大神解答 NO.2
f=lambda d,x:any(ord(t)&96==x for t in d) checkio=lambda d:f(d,32)&f(d,64)&f(d,96)&(len(d)>9)原諒我能力不夠,看不懂大神寫的這代碼是啥意思
大神解答 NO.3
checkio=lambda s:len(s)>9and all(any(f(c)for c in s)for f in[str.isdigit,str.isupper,str.islower])總結
以上是生活随笔為你收集整理的【Python CheckiO 题解】House Password的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 别拿父母的健康道德绑架我
- 下一篇: 家喻户晓的国产手机品牌乐视归来!499元