Saturday, December 14, 2019

Validating Postal Codes - Hacker Rank Solution

Validating Postal Codes - Hacker Rank Solution

Approach is:
a) (?=(\d)\d\1) using this regex findall 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.

import re
P = raw_input()
print len(re.findall(r'(?=(\d)\d\1)',P)) < 2 and bool(re.match(r'^[1-9][0-9]{5}$',P))

No comments:

Post a Comment

Powered by Blogger.