'Counting valid passwords in a list
I have a list
passwords = ['baCr',
'/)T.I]DdLNh)^WRIDa_z1',
'izM!Imi',
'NOb5VEhw$[17Q{P',
'i',
'J$52UY$Y',
'T',
'3',
'{77Vt3=2WOCpKWN?',
'$*K',
'[email protected](i;?',
'zr',
'/}YrhZ7T,X^/}cM0BvR@S)Le$/$Yg@',
'XUGc%',
'+$?D]',
'4?!-G}',
'iMI!G5q]bwI-fE1VT{8rsHWX2tk$29',
'E@',
'lVSh;W9:RyM%Zl9L*;',
'vWYJ%-v?^d:(o',
'.(tp?Hxu{[D+cAhmXGM{ahV3a_fZ!',
'3SXUCASo8/3psxL0p5mw=uf',
'%%7h',
'e(d',
'Ef98S2EUy)mT9MU{@F:zE^8Pb]mg)k',
'k',
'ji[Wtw.#9F$GN%ymi-FU',
'HX?z74lGp]TLqlGp[cRV!w(5]s?7m9',
'5eL',
'Zcrod:Xm',
'}t8bj:RJ4BA6i+x$PlbKL$NNIj',
'H)u(7{lK?Mk!cvUBkzlHXfFyeTL%Sh',
'/]AA-R0A',
'Qq1L$:(s',
'HoDUEPlaKAnDo2;W5}FlY)H(9I',
't0UVz3xgM2Js',
'XJ=@Y=P)z@%7',
'hnDTB7vtCa!MsAIR',
'{R/BvTB?iNS!vKntp$,tvwIC7',
'}2Lz.K',
'P9?+AgbGhp${uMoLqwS(aajG9P.b',
'eldDgR6SZ:v6w^G6ru*vo-',
'-{2{4',
'A=',
'Z{G2p{-#Kz/c}8utFm=WPH_:uTQiT',
'Ql91',
'[email protected]/}XA0TV4%uRi_k43',
'zcy.Vqfk2xpwgNt-KW?$m',
'X+O+NGT}',
'}]2_',
'Mcuz1;xCE{iHRhsO695ef',
';Q4!nRV7[,LvLDRjke1',
'3BJi9XZW',
'+Rot!',
'nP.kqo1-g2G8ZT$2wxRwI8MAMJ',
'^,$^F7:X=DkDUa?3,L{wf',
'56',
'#O/7CHV4WoMRmq^j$J$/Hk6DUD',
'WkWI5O1J017)5.%!dAaVLhoW',
')$;',
'{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F,R=?[^',
'[RiM[xNb',
'=I',
':M[Z%WY,w0.:{c;69ESv_@4S)#n',
'D6-9s.,f=pXiuMF;!l}!N]',
'L?2',
'=#l4,5',
'ZAdAEkQzdmc[O[A,H%9]3}i?',
'0%,OVJ9/}([L[Nq=3mgS:sYHa;',
'Y4:*NU',
'$To.Ce7:hd#YgKFZr+1i.Lx)BIuH',
'nM)@%*H9GiXevMmVm.(Z)]:!BxU!r',
'h?(Y_',
'F(qqHrvx5kL[Xx=4lcs:H!A',
'TgIT+I;GClgST/NB*',
'6=}.',
'+4',
'+BQ5C}:6',
'Dh5VF2Ek#a',
'REB4k%v.c1J*vq?F3#2',
'8}JCK',
'#{?2[X?M[A!_O]RVKqVv%93_@]vx,$',
'H44z_DwaG/A,g)xBCG+9GjAh7e]$!',
'jgct7fezi;jtqc*',
'(_',
'.3:',
'eM4mx)S9HDq_,*rZ;LE0S=U4L2i',
'-]F]']
and I need to find passwords meeting all of the following criteria:
- Must not contain any more than 15 characters
- Cannot contain any lowercase characters (a-z)
- The first and last character cannot be the same
This is my code:
valid=0
for password in passwords:
if len(password)<=15 and password.upper() and password[0]!=password[-1]: valid+=1
I believe the answer is 38, but I keep getting 85? Does anyone see an error in my code that would return the wrong answer? Thanks.
Solution 1:[1]
password.upper()
returns the password in caps. You need to check they are the same with password.upper() == password
:
valid = 0
for password in passwords:
if len(password) <= 15 and password.upper() == password and password[0] != password[-1]:
valid += 1
>>> valid
38
You can see what is happening here:
>>> x = "abc"
# Check the truthiness of x (this is what you were originally doing)
# A non-empty string is always True
>>> bool(x)
True
# x.upper() will just return an uppercase copy
>>> x.upper()
'ABC'
# You need to compare the original with the upper
>>> bool(x == x.upper())
False
Solution 2:[2]
You could use a regex to check if no lowercase characters (a-z) are present:
import re
passwords = [
'baCr',
'/)T.I]DdLNh)^WRIDa_z1',
'izM!Imi',
'NOb5VEhw$[17Q{P',
'i',
...
]
valid = 0
for password in passwords:
if len(password) <= 15 and not re.search('[a-z]', password) and password[0] != password[-1]:
valid += 1
print(f'{valid = }')
Output:
valid = 38
Solution 3:[3]
Another version:
import re
valid, pat = 0, re.compile(r"([^a-z])[^a-z]{,13}[^\1a-z]$")
for p in passwords:
valid += bool(pat.match(p))
print(valid)
Prints:
38
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Freddy Mcloughlan |
Solution 2 | |
Solution 3 | Andrej Kesely |