01 Jun Validating Postal Codes!
Hello Friends, How are you? Today I am going to solve the HackerRank Python Validating Postal Codes Problem with a straightforward explanation. In this article, you will get more than one approach to solve this problem. So let’s start- {tocify} $title={Table of Contents} A valid postal code P have to fulfil both below requirements: 1. P must be a number in the range of 100000 to 999999 inclusive. 2. 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 with just a single digit between them.
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. {codeBox}
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 ranging from 100000 to 999999 inclusive regex_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 P 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) {codeBox}
The locked stub code in the editor reads a single string denoting P from stdin and uses provided expression and your regular expressions to validate if P is a valid postal code. You are not responsible for printing anything to stdout. Locked stub code in the editor does that.
110000 {codeBox}
False {codeBox}
1 1 0000 : (0, 0) and (0, 0) are two alternating digit pairs. Hence, it is an invalid postal code. A score of 0 will be awarded for using the ‘if’ conditions in your code. You have to pass all the test cases to get a positive score. a) (?=(\d)\d\1) using this regex find how many alternating repetitive digits are there. b) ^[1-9][0-9]{5}$ using this regex check that postal code is in the range 100000 – 999999 Add the boolean obtained from these to checks and print the result. Approach I: Validating Postal Codes HackerRank Python Solution
# ========================
# Information
# ======================== # Name: Validating Postal Codes - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/validating-postalcode/problem
# Difficulty: Hard
# Max Score: 80
# Cutoff Score: 40.00
# Language: Python 3 # ========================
# Solution Start
# ======================== # Validating Postal Codes - Hacker Rank Solution Start # Code Starts Here
regex_integer_in_range = r"^[1-9][\d]{5}$" # Do not delete 'r'.
regex_alternating_repetitive_digit_pair = r"(\d)(?=\d\1)" # Do not delete 'r'.
# Code Ends Here import re
P = input() print (bool(re.match(regex_integer_in_range, P)) and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2) # Validating Postal Codes in Python - Hacker Rank Solution END
# MyEduWaves
Sorry, the comment form is closed at this time.