Find the single digit sum of alphabetical values of a string | Python program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code
def findTheSum ( str ) : NEW_LINE INDENT alpha = " " NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT if ( ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) or ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) ) : NEW_LINE INDENT alpha += str [ i ] NEW_LINE DEDENT DEDENT score = 0 NEW_LINE n = 0 NEW_LINE for i in range ( 0 , len ( alpha ) ) : NEW_LINE INDENT if ( alpha [ i ] >= ' A ' and alpha [ i ] <= ' Z ' ) : NEW_LINE INDENT score += ord ( alpha [ i ] ) - ord ( ' A ' ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT score += ord ( alpha [ i ] ) - ord ( ' a ' ) + 1 NEW_LINE DEDENT DEDENT while ( score > 0 or n > 9 ) : NEW_LINE INDENT if ( score == 0 ) : NEW_LINE INDENT score = n NEW_LINE n = 0 NEW_LINE DEDENT n += score % 10 NEW_LINE score = score // 10 NEW_LINE DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " GeeksforGeeks " NEW_LINE print ( findTheSum ( S ) ) NEW_LINE DEDENT