Python program to find the string weight
Last Updated :
15 Apr, 2023
Given a String, each character mapped with a weight( number), compute the total weight of the string.
Input : test_str = 'GeeksforGeeks',
{"G" : 1, "e" : 2, "k" : 5, "f" : 3, "s" : 15, "o" : 4, "r" : 6}
Output : 63
Explanation : 2 (G*2) + 8(e*4) + 30(s*2) + 10(k*2) + 4(o) + 6(r) +3(f) = 63.
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15}
Output : 25
Method#1: Using loop
This is one of the ways in which this task can be performed. In this, we iterate for all the characters and sum all the weights mapped from dictionary.
Python3
test_dict = { 'tough' : 1 , 'to' : 2 , 'do' : 3 , 'todays' : 4 , 'work' : 5 }
print ( "The original dictionary : " + str (test_dict))
test_pref = 'to'
res = {key: val for key, val in test_dict.items()
if key.startswith(test_pref)}
print ( "Filtered dictionary keys are : " + str (res))
|
Output
The original string is : GeeksforGeeks
The weighted sum : 81
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using sum()
This is one more way in which this task can be performed. In this, we use generator expression, and sum() is used to compute the summation of individual weights.
Python3
test_dict = { 'tough' : 1 , 'to' : 2 , 'do' : 3 , 'todays' : 4 , 'work' : 5 }
print ( "The original dictionary : " + str (test_dict))
test_pref = 'to'
res = dict ( filter ( lambda item: item[ 0 ].startswith(test_pref),
test_dict.items()))
print ( "Filtered dictionary keys are : " + str (res))
|
Output
The original string is : GeeksforGeeks
The weighted sum : 81
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using list(),set(),count() methods
Python3
test_str = 'GeeksforGeeks'
print ( "The original string is : " + str (test_str))
sum_dict = { "G" : 5 , "e" : 2 , "k" : 10 ,
"f" : 3 , "s" : 15 , "o" : 4 , "r" : 6 }
res = 0
x = list ( set (test_str))
for i in x:
res + = (test_str.count(i) * sum_dict[i])
print ( "The weighted sum : " + str (res))
|
Output
The original string is : GeeksforGeeks
The weighted sum : 81
Approach#4: using for loop:
- Define a function that takes two arguments, the input string and the weight dictionary.
- Initialize a variable “weight” to 0.
- Iterate through each character in the string.
- If the character is present in the weight dictionary, add its weight to the “weight” variable.
- Return the “weight” variable.
Python3
def calculate_string_weight(test_str, weight_dict):
weight = 0
for char in test_str:
if char in weight_dict:
weight + = weight_dict[char]
return weight
test_str = 'GeeksforGeeks'
weight_dict = { "G" : 1 , "e" : 2 , "k" : 5 , "f" : 3 , "s" : 15 , "o" : 4 , "r" : 6 }
print (calculate_string_weight(test_str, weight_dict))
test_str = 'Geeks'
weight_dict = { "G" : 1 , "e" : 2 , "k" : 5 , "s" : 15 }
print (calculate_string_weight(test_str, weight_dict))
|
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(1), as we are not using any additional space proportional to the input size.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...