Nambiar Number Generator | Function to return the Nambiar number of the given number ; If there is no digit to choose ; Choose the first digit ; Chosen digit 's parity ; To store the sum of the consecutive digits starting from the chosen digit ; While there are digits to choose ; Update the sum ; If the parity differs ; Return the current sum concatenated with the Numbiar number for the rest of the String ; Driver code
def nambiarNumber ( Str , i ) : NEW_LINE INDENT if ( i >= len ( Str ) ) : NEW_LINE INDENT return " " NEW_LINE DEDENT firstDigit = ord ( Str [ i ] ) - ord ( '0' ) NEW_LINE digitParity = firstDigit % 2 NEW_LINE sumDigits = 0 NEW_LINE while ( i < len ( Str ) ) : NEW_LINE INDENT sumDigits += ( ord ( Str [ i ] ) - ord ( '0' ) ) NEW_LINE sumParity = sumDigits % 2 NEW_LINE if ( digitParity != sumParity ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( " " + str ( sumDigits ) + nambiarNumber ( Str , i + 1 ) ) NEW_LINE DEDENT Str = "9880127431" NEW_LINE print ( nambiarNumber ( Str , 0 ) ) NEW_LINE