Find the next number by adding natural numbers in order on alternating indices from last | Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
def generateNumber ( number ) : NEW_LINE INDENT temp = 0 ; adding_number = 0 ; NEW_LINE result = " " ; NEW_LINE l = len ( number ) ; NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT digit = ord ( number [ i ] ) - ord ( '0' ) ; NEW_LINE if ( temp % 2 == 0 ) : NEW_LINE INDENT adding_number += 1 ; NEW_LINE digit += adding_number ; NEW_LINE if ( digit >= 10 ) : NEW_LINE INDENT digit %= 9 ; NEW_LINE if ( digit == 0 ) : NEW_LINE INDENT digit = 9 ; NEW_LINE DEDENT DEDENT DEDENT result = str ( digit ) + result ; NEW_LINE temp += 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1345" ; NEW_LINE print ( generateNumber ( S ) ) ; NEW_LINE DEDENT