In this HackerRank Validating Postal Codes problem solution, 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 inclusive
- regex_alternating_repetitive_digit_pair should find alternating repetitive digits pairs in a given string.
HackerRank Validating Postal Codes in python problem solution
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
def in_range(s):
return (int(s) < 10**6) and (int(s) >= 10**5)
def pairs(s):
r = 0
n = len(s)
for i in xrange(n-2):
r += int((i < 3 and s[i]==s[i+2]) or (i > 2 and s[i]==s[i+2] and s[i-2] != s[i]))
return (r < 2)
def solve(s):
return is_number(s) and in_range(s) and pairs(s)
s = raw_input()
print str(solve(s))
1 Comments
don't use regular expressions
ReplyDelete