Suppose we have a postal code we have to check whether it is valid or not. A valid postal code has following criteria
It must be a number in the range from 100000 to 999999 (both inclusive).
It must not contain more than one alternating repetitive digit pair.
So, if the input is like s = "700035", then the output will be True as this is in range 100000 to 999999 and there are no consecutive digits either.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding
def solve(s): n = len(s) nb = 0 ok = True for i in range(n): ok = ok and s[i].isdigit() for i in range(n-2): nb += s[i] == s[i+2] return ok and n == 6 and s != '0' and nb < 2 s = "700035" print(solve(s))