We are given a sentence of english language(can also contain digits), we need to compute and print the sum of ASCII values of characters of each word in that sentence.
Input : GeeksforGeeks, a computer science portal for geeks Output : Sentence representation as sum of ASCII each character in a word: 1361 97 879 730 658 327 527 Total sum -> 4579 Here, [GeeksforGeeks, ] -> 1361, [a] -> 97, [computer] -> 879, [science] -> 730 [portal] -> 658, [for] -> 327, [geeks] -> 527 Input : I am a geek Output : Sum of ASCII values: 73 206 97 412 Total sum -> 788
This problem has existing solution please refer Sums of ASCII values of each word in a sentence link. We will solve this problem quickly in python using map() function and Dictionary data structures. Approach is simple,
- First split all words in sentence separated by space.
- Create a empty dictionary which will contain word as key and sum of ASCII values of it’s characters as value.
- Now traverse list of splitted words and for each word map ord(chr) function on each character of current word and them calculate sum of ascii values of each character of current word.
- While traversing each word map sum of ascii values on it’s corresponding word in resultant dictionary created above.
- Traverse splitted list of words and print their corresponding ascii value by looking up into resultant dictionary.
- Python - Filter dictionary values in heterogenous dictionary
- Python - Extract Unique values dictionary values
- Python - Remove duplicate values across Dictionary Values
- Python | Convert String list to ascii values
- Python dictionary | values()
- Python - Dictionary Values Division
- Python | Initialize dictionary with None values
- Python | Max/Min of tuple dictionary values
- Python | Selective key values in dictionary
- Python | Check if all values are 0 in dictionary
- Python - Associated Values Frequencies in Dictionary
- Python - Unique Values of Key in Dictionary
- Python | Check for None values in given dictionary
- Python - Smallest K values in Dictionary
- Python - Check if all values are K in dictionary
- Python - Keys associated with Values in Dictionary
- Python | Even values update in dictionary
- Python - Mapping key values to Dictionary
- Python | Sum values for each key in nested dictionary
- Python | N largest values in dictionary
Sum of ASCII values: 1361 97 879 730 658 327 527 Total sum -> 4579
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.