Open In App

Map function and Dictionary in Python to sum ASCII values

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given a sentence in the English language(which can also contain digits), and we need to compute and print the sum of ASCII values of the 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 an existing solution please refer to 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.

Python3




# 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 splitted 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 program
if __name__ == "__main__":
    sentence = 'I am a geek'
    asciiSums(sentence)


Output:

Sum of ASCII values:
1361 97 879 730 658 327 527
Total sum -> 4579



Previous Article
Next Article

Similar Reads

Substring with maximum ASCII sum when some ASCII values are redefined
Given a string W, and two arrays X[] and B[] of size N each where the ASCII value of character X[i] is redefined to B[i]. Find the substring with the maximum sum of the ASCII (American Standard Code for Information Interchange) value of the characters. Note: Uppercase & lowercase both will be present in the string W. Input: W = "abcde", N = 1,
9 min read
ASCII NULL, ASCII 0 ('0') and Numeric literal 0
The ASCII (American Standard Code for Information Interchange) NULL and zero are represented as 0x00 and 0x30 respectively. An ASCII NULL character serves as a sentinel character of strings in C/C++. When the programmer uses '0' in his code, it will be represented as 0x30 in hex form. What will be filled in the binary integer representation in the
3 min read
What is ASCII - A Complete Guide to Generating ASCII Code
The American Standard Code for Information Interchange, or ASCII, is a character encoding standard that has been a foundational element in computing for decades. It plays a crucial role in representing text and control characters in digital form. Historical BackgroundASCII has a rich history, dating back to its development in the early 1960s. Origi
13 min read
Check if a string contains a non-ASCII value in Julia - ascii() Method
The ascii() is an inbuilt function in Julia which is used to convert a specified string to a String type and also check the presence of ASCII data. If the non-ASCII byte is present, throw an ArgumentError indicating the position of the first non-ASCII byte. Syntax: ascii(s::AbstractString) Parameters:s::AbstractString: Specified stringReturns: It r
1 min read
Python - Append Dictionary Keys and Values ( In order ) in dictionary
Given a dictionary, perform append of keys followed by values in list. Input : test_dict = {"Gfg" : 1, "is" : 2, "Best" : 3} Output : ['Gfg', 'is', 'Best', 1, 2, 3] Explanation : All the keys before all the values in list. Input : test_dict = {"Gfg" : 1, "Best" : 3} Output : ['Gfg', 'Best', 1, 3] Explanation : All the keys before all the values in
5 min read
Python program to find the sum of Characters ascii values in String List
Given the string list, the task is to write a Python program to compute the summation value of each character's ASCII value. Examples: Input : test_list = ["geeksforgeeks", "teaches", "discipline"] Output : [133, 61, 100] Explanation : Positional character summed to get required values. Input : test_list = ["geeksforgeeks", "discipline"] Output : [
4 min read
Python program to update a dictionary with the values from a dictionary list
Given a dictionary and dictionary list, update the dictionary with dictionary list values. Input : test_dict = {"Gfg" : 2, "is" : 1, "Best" : 3}, dict_list = [{'for' : 3, 'all' : 7}, {'and' : 1, 'CS' : 9}] Output : {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'and': 1, 'CS': 9} Explanation : All dictionary keys updated in single dictionary. I
8 min read
Python - Filter dictionary values in heterogeneous dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to filter out certain values based on certain conditions on a particular type, e.g all values smaller than K. This task becomes complex when dictionary values can be heterogeneous. This kind of problem can have applications across many domains. Let's discuss c
6 min read
Count the number of words having sum of ASCII values less than and greater than k
Given a string, the task is to count the number of words whose sum of ASCII values is less than and greater than or equal to given k. Examples: Input: str = "Learn how to code", k = 400Output:Number of words having the sum of ASCII less than k = 2Number of words having the sum of ASCII greater than or equal to k = 2 Input: str = "Geeks for Geeks",
11 min read
Queries to calculate sum of squares of ASCII values of characters of a substring with updates
Given a string S of length N and a 2D array of strings Q[][] consisting of M queries of the following type: Query("U", "I", "X"): Update the character at index I with the character X.Query("S", "L", "R"): Print the sum of the squares of the position of the characters in the substring {S[L], ...., S[R]} according to the English alphabets. Examples:
15+ min read
Article Tags :
Practice Tags :