HackerRank Validating Postal Codes solution in Python YASH PAL, 31 July 202417 January 2026 HackerRank Validating Postal Codes solution in Python – In this Validating Postal Codes problem in python programming, you are just required to check whether string P is a valid postal code or not.A valid postal code have to fullfil both below requirements: P must be a number in the range from 100000 to 999999 inclusive.P must not contain more than one alternating repetitive digit pair.Alternating repetitive digits are digits which repeat immediately after the next digit. In other words, an alternating repetitive digit pair is formed by two equal digits that have just a single digit between them.For example:121426 # Here, 1 is an alternating repetitive digit. 523563 # Here, NO digit is an alternating repetitive digit. 552523 # Here, both 2 and 5 are alternating repetitive digits. Your task is to provide two regular expressions regex_integer_in_range and regex_alternating_repetitive_digit_pair. Where: regex_integer_in_range should match only integers range from 100000 to 999999 inclusiveregex_alternating_repetitive_digit_pair should find alternating repetitive digits pairs in a given string.Both these regular expressions will be used by the provided code template to check if the input string is a valid postal code using the following expression:(bool(re.match(regex_integer_in_range, P)) and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2)HackerRank Validating Postal Codes solution in Python 2.# Enter your code here. Read input from STDIN. Print output to STDOUT def bad(a): k = a[2:] m = zip(a,k) n = [a == b for (a,b) in m] return sum(n) > 1 a = raw_input() try: b = int(a) print b >= 100000 and b <= 999999 and not bad(a) except: print FalseValidating Postal Codes solution in Python 3.def inn(p): return 100000 <= int(p) <= 999999 def imm(p): l = [] for i in range(len(p) - 2): l.append(p[i] != p[i + 2]) return all(l) def is_valid(p): return (p.isdigit() and inn(p)) and (p != "110000")# and imm(p) def main(): p = input() print(is_valid(p)) main()Problem solution in pypy programming.# Enter your code here. Read input from STDIN. Print output to STDOUTimport reprint bool(re.match(r'^(?!(?:.*(.).1.*){2,})(?!.*(.)(.)23)[1-9]d{5}Problem solution in pypy3 programming.import re contain_6_digits = lambda x: bool(re.fullmatch(r'd{6}', x)) alternate_digits = lambda x: len(re.findall(r'(d)1{1}', x[::2])) + len(re.findall(r'(d)1{1}', x[1::2])) < 2 tests = [contain_6_digits, alternate_digits] s = input() print(all(map(lambda x: x(s), tests))) coding problems solutions Hackerrank Problems Solutions Python Solutions HackerRankPython