Map function and Dictionary in Python to sum ASCII values - GeeksforGeeks

13 Dec, 2017
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.
Examples:
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.# Function to find sums of ASCII values of each # word in a sentence in  def asciiSums(sentence):      # split words separated by space    words = sentence.split(' ')      # create empty dictionary    result = {}      # calculate sum of ascii values of each word    for word in words:         currentSum = sum(map(ord,word))           # map sum and word into resultant dictionary         result[word] = currentSum      totalSum = 0      # iterate list of splited words in order to print    # sum of ascii values of each word sequentially    sumsOfAscii = [result[word] for word in words]    print ('Sum of ASCII values:')    print (' '.join(map(str,sumsOfAscii)))    print ('Total Sum -> ',sum(sumsOfAscii))  # Driver programif __name__ == "__main__":    sentence = 'I am a geek'    asciiSums(sentence)Output:Sum of ASCII values:
1361 97 879 730 658 327 527 
Total sum -> 4579
My Personal Notes
arrow_drop_upSave
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.# Function to find sums of ASCII values of each # word in a sentence in  def asciiSums(sentence):      # split words separated by space    words = sentence.split(' ')      # create empty dictionary    result = {}      # calculate sum of ascii values of each word    for word in words:         currentSum = sum(map(ord,word))           # map sum and word into resultant dictionary         result[word] = currentSum      totalSum = 0      # iterate list of splited words in order to print    # sum of ascii values of each word sequentially    sumsOfAscii = [result[word] for word in words]    print ('Sum of ASCII values:')    print (' '.join(map(str,sumsOfAscii)))    print ('Total Sum -> ',sum(sumsOfAscii))  # Driver programif __name__ == "__main__":    sentence = 'I am a geek'    asciiSums(sentence)Output:Sum of ASCII values:
1361 97 879 730 658 327 527 
Total sum -> 4579
My Personal Notes
arrow_drop_upSave
# Function to find sums of ASCII values of each # word in a sentence in  def asciiSums(sentence):      # split words separated by space    words = sentence.split(' ')      # create empty dictionary    result = {}      # calculate sum of ascii values of each word    for word in words:         currentSum = sum(map(ord,word))           # map sum and word into resultant dictionary         result[word] = currentSum      totalSum = 0      # iterate list of splited words in order to print    # sum of ascii values of each word sequentially    sumsOfAscii = [result[word] for word in words]    print ('Sum of ASCII values:')    print (' '.join(map(str,sumsOfAscii)))    print ('Total Sum -> ',sum(sumsOfAscii))  # Driver programif __name__ == "__main__":    sentence = 'I am a geek'    asciiSums(sentence)Output:Sum of ASCII values:
1361 97 879 730 658 327 527 
Total sum -> 4579
My Personal Notes
arrow_drop_upSave
# Function to find sums of ASCII values of each # word in a sentence in  def asciiSums(sentence):      # split words separated by space    words = sentence.split(' ')      # create empty dictionary    result = {}      # calculate sum of ascii values of each word    for word in words:         currentSum = sum(map(ord,word))           # map sum and word into resultant dictionary         result[word] = currentSum      totalSum = 0      # iterate list of splited words in order to print    # sum of ascii values of each word sequentially    sumsOfAscii = [result[word] for word in words]    print ('Sum of ASCII values:')    print (' '.join(map(str,sumsOfAscii)))    print ('Total Sum -> ',sum(sumsOfAscii))  # Driver programif __name__ == "__main__":    sentence = 'I am a geek'    asciiSums(sentence)
Output:
Sum of ASCII values:
1361 97 879 730 658 327 527 
Total sum -> 4579

ASCII
Python
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
Python OOPs Concepts
Read a file line by line in Python
How to Install PIP on Windows ?
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
C++ Data Types
Write a program to print all permutations of a given string