#45230: python思路+題解


chenwei980503@gmail.com (陳維(Z))

學校 : 新北市立北大高級中學
編號 : 278351
來源 : [49.216.26.72]
最後登入時間 :
2025-03-12 17:25:19
a624. 4. Password Analyzer -- HP CodeWars2007 | From: [49.216.20.87] | 發表日期 : 2025-01-29 21:14

應用python語法, 快速解
  1. 檢查密碼特性:

    • 長度檢查: 使用 len(p) >= 8 來確認密碼的長度是否至少為 8 個字符,結果存儲在 le 中。
    • 大寫字母檢查: 使用 any(c.isupper() for c in p) 來確認是否至少有一個字符是大寫字母,結果存儲在 u 中。
    • 小寫字母檢查: 使用 any(c.islower() for c in p) 來確認是否至少有一個字符是小寫字母,結果存儲在 lo 中。
    • 數字或符號檢查: 使用 any(c.isdigit() or not c.isalnum() for c in p) 來確認密碼中是否包含至少一個數字或符號,結果存儲在 n_s 中。這裡的 not c.isalnum() 用於檢查符號。
  2. 計算符合的特性數量: 使用 sum([le, u, lo, n_s]) 獲得符合的特性數量,並將其存儲在變量 c 中。

  3. 評分類別:

    • 如果 c 等於 4,則打印 "This password is STRONG"。
    • 如果 c 等於 3,則打印 "This password is GOOD"。
    • 如果 c 等於 2,則打印 "This password is ACCEPTABLE"。
    • 如果 c 等於 1 或 0,則打印 "This password is WEAK"。

while True:
try:
p=input() # p: password
le=len(p)>=8 # le: length
u=any(c.isupper() for c in p) # u: uppercase letter
lo=any(c.islower() for c in p) # lo: lowercase letters
n_s=any(c.isdigit() or not c.isalnum() for c in p) # n_s: numbers and symbols
c=sum([le, u, lo, n_s])
if c==4:
print("This password is STRONG")
elif c==3:
print("This password is GOOD")
elif c==2:
print("This password is ACCEPTABLE")
else:
print("This password is WEAK")
except:
break
 
ZeroJudge Forum