Find the element at the specified index of a Spirally Filled Matrix | Function to return the element at ( x , y ) ; If y is greater ; If y is odd ; If y is even ; If x is even ; If x is odd ; Driver code
def SpiralElement ( x , y ) : NEW_LINE INDENT r = 0 NEW_LINE if ( x < y ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT r = y * y NEW_LINE return ( r - x + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT r = ( y - 1 ) * ( y - 1 ) NEW_LINE return ( r + x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( x % 2 == 0 ) : NEW_LINE INDENT r = x * x NEW_LINE return ( r - y + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT r = ( x - 1 ) * ( x - 1 ) NEW_LINE return ( r + y ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 2 NEW_LINE y = 3 NEW_LINE print ( SpiralElement ( x , y ) ) NEW_LINE DEDENT