We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This solution takes the input string, s, and checks if it satisfies any of the five str methods using a for loop and the built-in any function. The lambda function is used to dynamically access the is<method> methods of each character in s. This solution demonstrates the Don't Repeat Yourself (DRY) principle by not explicitly calling each of the is<method> methods separately.
Solution 2:
fromreimportsearchfromtypingimportCallabledefpattern_match(input_string:str)->Callable[[str],bool]:"""Return a pattern matching function."""def_pattern_match(pattern:str)->bool:"""Call re.search with the given pattern and cast to bool."""returnbool(search(rf"[{pattern}]", input_string))return_pattern_matchif__name__=="__main__":print(*map(pattern_match(input()),(r"^\W_",r"a-zA-Z",r"\d",r"a-z",r"A-Z"),),sep="\n",)
This solution defines a pattern_match function that returns a closure that takes a pattern and returns a boolean indicating if the pattern matches the input string. The function uses re.search to check if a character in the input string matches the pattern. This solution demonstrates the advantage of using closures to encapsulate functionality and avoid code duplication.
Solution 3:
fromreimportsearchfromtypingimportCallabledefpattern_match(input_string:str)->Callable[[str],bool]:"""Return a pattern matching function."""def_pattern_match(pattern:str)->bool:"""Call re.search with the given pattern and cast to bool."""returnbool(search(rf"[{pattern}]", input_string))return_pattern_matchif__name__=="__main__":digit,lower,upper=map(pattern_matcher:=pattern_match(input()),(r"\d",r"a-z",r"A-Z"))print((alpha:=lowerorupper)ordigitorpattern_matcher(r"^\W_"),alpha,digit,lower,upper,sep="\n",)
This solution is similar to Solution 2 but uses the short-circuiting behavior of the or operator to avoid redundant function calls. If one of the lower or upper conditions is True, the digit condition is not evaluated. Similarly, if the alpha condition is True, pattern_matcher is not called a fourth time.
String Validators
You are viewing a single comment's thread. Return to all comments →
Python version: 3.8+
Solution 1:
This solution takes the input string,
s, and checks if it satisfies any of the fivestrmethods using a for loop and the built-inanyfunction. Thelambdafunction is used to dynamically access theis<method>methods of each character ins. This solution demonstrates the Don't Repeat Yourself (DRY) principle by not explicitly calling each of theis<method>methods separately.Solution 2:
This solution defines a
pattern_matchfunction that returns a closure that takes a pattern and returns a boolean indicating if the pattern matches the input string. The function usesre.searchto check if a character in the input string matches the pattern. This solution demonstrates the advantage of using closures to encapsulate functionality and avoid code duplication.Solution 3:
This solution is similar to Solution 2 but uses the short-circuiting behavior of the
oroperator to avoid redundant function calls. If one of thelowerorupperconditions isTrue, thedigitcondition is not evaluated. Similarly, if thealphacondition isTrue,pattern_matcheris not called a fourth time.Code-golfed solution (101 characters):