Advertisement
Guest User

Password validator

a guest
Sep 16th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. # Created by Cépagrave [busyCarpenting]
  2.  
  3. print((lambda p:4<len(p)<11*(1-p.isalnum())*(len(p.split())<2)*bool({*p}&{*'0123456789'}))(input()))
  4.  
  5. """
  6. Explanation of this code:
  7.  
  8. the challenge rules are:
  9.  
  10. 1 - min length of 5
  11. 2 - max length of 10
  12. 3 - at least 1 digit
  13. 4 - at least 1 special character
  14. 5 - no space
  15.  
  16. // First, the code global structure is:
  17.  
  18. print((lambda p: -- long expression -- )(input()))
  19.  
  20. the result here is: input is taken, passed as "p" argument to the lambda, this argument is used in the long expression, which will return a boolean (True or False)
  21.  
  22. // Now, the long expression:
  23. 4<len(p)<11*(1-p.isalnum())*(len(p.split())<2)*bool({*p}&{*'0123456789'})
  24.  
  25. the center of it is:
  26.  
  27. 4<len(p)<11
  28. it will return True if rules (1) and (2) are respected
  29.  
  30. on the right, 3 elements are multiplying 11:
  31. 11*(--1--)*(--2--)*bool(--3--)
  32. if one of the 3 elements is evaluated to False (or 0), then 11*..*..*.. will take the value 0, and len(p)<0 will return "False"
  33.  
  34. // So now, the 3 elements:
  35.  
  36. (1-p.isalnum()) will give 0 if the password is only made of alphanumeric characters (a,b,c,d,...0,1,2,3,...) => special characters needed, or else 1-p.isalphanum() takes value 0 => 11*0=0, len(p)<0 returns False
  37.  
  38. (len(p.split())<2) will give 0 if there is one or more space in the password => 11*0, etc...
  39.  
  40. bool({*p}&{*'0123456789'}) this is evaluating a sets intersection:
  41. {*p} is the set of characters in p
  42. {*'0123456789'} is the set of string digits
  43. if they share one element, it will be in the intersection, and bool({"5"}) for ex will evaluate to True, while bool(--empty set--) will evaluate to False, thus 0, => 11*0, etc...
  44. """
  45.  
  46. # old version:
  47. # print((lambda p:bool(4<len(p)<11 and set(p)&{*'0123456789'}and not(p.isalnum()+len(p.split())>1))*'Ok'or'No')(input()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement