Check is rowNum is 0 or n - 1
if ( rowNum == 0 or rowNum == n - 1 ) : NEW_LINE INDENT i += ( 2 * n - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( up ) : NEW_LINE INDENT i += ( 2 * ( n - rowNum ) - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT i += rowNum * 2 NEW_LINE DEDENT up ^= True NEW_LINE DEDENT return result NEW_LINE