Sort by

recency

|

313 Discussions

|

  • + 0 comments
    import re
    
    # Regular expression to match integers in the range 100000 to 999999 inclusive
    regex_integer_in_range = r"^[1-9][0-9]{5}$"  # Matches a 6-digit number starting with a non-zero digit.
    
    # Regular expression to find alternating repetitive digit pairs
    regex_alternating_repetitive_digit_pair = r"(?=(\d)\d\1)"  # Lookahead for digit patterns like 121, 232, etc.
    
    # Input reading
    P = input().strip()
    
    # Validation: Check the postal code
    is_valid = (bool(re.match(regex_integer_in_range, P)) and 
                len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2)
    
    # Output result
    print(is_valid)
    
    
    
    import re
    P = input()
    
    print (bool(re.match(regex_integer_in_range, P)) 
    and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2)
    
  • + 1 comment

    \1 can be used inside of the lookahead even if the group is declared outside of it, wich enhance visibility and compehensiveness in that case.

    regex_integer_in_range = r"^[1-9]{1}[0-9]{5}$"
    regex_alternating_repetitive_digit_pair = r"(.)(?=.\1)"
    
    
    import re
    P = input()
    
    print (bool(re.match(regex_integer_in_range, P)) 
    and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2)
    
  • + 0 comments
    regex_integer_in_range = r"^[1-9]\d{5}$"
    regex_alternating_repetitive_digit_pair = r"(?=(.).\1)"
    import re
    P = input()
    print (bool(re.match(regex_integer_in_range, P)) 
    and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2)
    
  • + 0 comments
    regex_integer_in_range = r"^[0-9]{6}$"
    regex_alternating_repetitive_digit_pair = r"([0-9]{1})(?=[0-9]{1}\1)"	
    
  • + 0 comments
    regex_integer_in_range = r"^[100000-999999]{6}$"	# Do not delete 'r'.
    
    # Not more than one ARD pair
    regex_alternating_repetitive_digit_pair = r"(\d)(?=\d\1)"	# Do not delete 'r'.
    
    '''
    source: StackOverflow
    
    Explanation:
    
    (\d): Match and capture a digit in group #1
    
    (?=: Start lookahead
    
    \d: Match any digit
    
    \1: Back-reference to captured group #1
    
    ): End lookahead
    '''
    
    
    import re
    P = input()
    
    print (bool(re.match(regex_integer_in_range, P)) 
    and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2)