In this Validating Postal Codes problem, you are just required to check whether string P is a valid postal code or not.


HackerRank Validating Postal Codes solution in python


Problem solution in Python 2 programming.

# 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 False


Problem solution in Python 3 programming.

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 STDOUT
import re

print bool(re.match(r'^(?!(?:.*(.).\1.*){2,})(?!.*(.)(.)\2\3)[1-9]\d{5}$', raw_input()))


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)))