Count the number of words having sum of ASCII values less than and greater than k | Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
def CountWords ( str , k ) : NEW_LINE INDENT sum = 0 NEW_LINE NumberOfWords = 0 NEW_LINE counter = 0 NEW_LINE l = len ( str ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( str [ i ] == ' ▁ ' ) : NEW_LINE INDENT if ( sum < k ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT sum = 0 NEW_LINE NumberOfWords += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += ord ( str [ i ] ) NEW_LINE DEDENT DEDENT NumberOfWords += 1 NEW_LINE if ( sum < k ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT print ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII " , " values ▁ less ▁ than ▁ k ▁ = " , counter ) NEW_LINE print ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII ▁ values " , " greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = " , NumberOfWords - counter ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " Learn ▁ how ▁ to ▁ code " NEW_LINE k = 400 NEW_LINE CountWords ( str , k ) NEW_LINE DEDENT