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.
importre# Regular expression to match integers in the range 100000 to 999999 inclusiveregex_integer_in_range=r"^[1-9][0-9]{5}$"#Matchesa6-digitnumberstartingwithanon-zerodigit.# Regular expression to find alternating repetitive digit pairsregex_alternating_repetitive_digit_pair=r"(?=(\d)\d\1)"#Lookaheadfordigitpatternslike121,232,etc.# Input readingP=input().strip()# Validation: Check the postal codeis_valid=(bool(re.match(regex_integer_in_range,P))andlen(re.findall(regex_alternating_repetitive_digit_pair,P))<2)# Output resultprint(is_valid)importreP=input()print(bool(re.match(regex_integer_in_range,P))andlen(re.findall(regex_alternating_repetitive_digit_pair,P))<2)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Validating Postal Codes
You are viewing a single comment's thread. Return to all comments →