Sum of numbers formed by consecutive digits present in a given string | Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver Code
def sumOfDigits ( s ) : NEW_LINE INDENT curr = 0 NEW_LINE ret = 0 NEW_LINE for ch in s : NEW_LINE INDENT if ( ord ( ch ) >= 48 and ord ( ch ) <= 57 ) : NEW_LINE INDENT curr = curr * 10 + ord ( ch ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT ret += curr NEW_LINE curr = 0 NEW_LINE DEDENT DEDENT ret += curr NEW_LINE return ret NEW_LINE DEDENT S = "11aa32bbb5" NEW_LINE print ( sumOfDigits ( S ) ) NEW_LINE