Alternating Numbers | Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Given Number N ; Function Call
def isEvenOddForm ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 and int ( s [ i ] ) % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i % 2 == 1 and int ( s [ i ] ) % 2 != 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isOddEvenForm ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 and int ( s [ i ] ) % 2 != 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i % 2 == 1 and int ( s [ i ] ) % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isAlternating ( n ) : NEW_LINE INDENT s = str ( n ) NEW_LINE return ( isEvenOddForm ( s ) or isOddEvenForm ( s ) ) NEW_LINE DEDENT N = 129 NEW_LINE if ( isAlternating ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT