Home  >  Article  >  Backend Development  >  Python program to find weight of string

Python program to find weight of string

WBOY
WBOYforward
2023-09-04 20:09:10958browse

Python program to find weight of string

In this article, the given task is to find the total weight of a string. To calculate the string weight, we convert the given string into a lower form. Considering the weight of characters, we take a=1, b=,2 and so on until z=26. In this Python article, a method for finding the weight of a given string is presented using two different examples. In the first example, the given characters in the string are fetch, hed and then their respective weights are added to the updated weights. In Example 2, you first calculate how often a given character appears in the string, then multiply that frequency by the corresponding character weight, and then add all these component weights together to get the final result.

Example 1: Use iteration to find string weights and add character weights.

algorithm

Step 1 - First make atoz = 'abcdefghijklmnopqrstuvwxyz'.

Step 2 - We will use atoz.index() function to get the weight number, for example here space ' ' will have value of 0, b will have value of 2 and so on.

Step 3 - Now specify the given string for which the string weight is to be calculated.

Step 4 - Iterate over the given string to get characters one by one.

Step 5 - Find the position value (weight value) of the character in atoz.

Step 6 - Update the string weight by adding the character's weight value.

Step 7 - Finally, print the total results.

Example

givenstr = 'this is a sample string'
def calculateWeight(teststr):
   teststr = teststr.lower()
   atoz = ' abcdefghijklmnopqrstuvwxyz'
   weight = 0
   for item in range(len(teststr)):
      elem = teststr[item]
      currweight = atoz.index(elem)
      weight += currweight
      print("This albhabet:",elem, ", alphabet weight:", currweight, ", Updated String Weight ", weight)
   return weight
finalresult= calculateWeight(givenstr)
print("Final String Weight: ",finalresult) 

Output

This albhabet: t , alphabet weight: 20 , Updated String Weight  20
This albhabet: h , alphabet weight: 8 , Updated String Weight  28
This albhabet: i , alphabet weight: 9 , Updated String Weight  37
This albhabet: s , alphabet weight: 19 , Updated String Weight  56
This albhabet:   , alphabet weight: 0 , Updated String Weight  56
This albhabet: i , alphabet weight: 9 , Updated String Weight  65
This albhabet: s , alphabet weight: 19 , Updated String Weight  84
This albhabet:   , alphabet weight: 0 , Updated String Weight  84
This albhabet: a , alphabet weight: 1 , Updated String Weight  85
This albhabet:   , alphabet weight: 0 , Updated String Weight  85
This albhabet: s , alphabet weight: 19 , Updated String Weight  104
This albhabet: a , alphabet weight: 1 , Updated String Weight  105
This albhabet: m , alphabet weight: 13 , Updated String Weight  118
This albhabet: p , alphabet weight: 16 , Updated String Weight  134
This albhabet: l , alphabet weight: 12 , Updated String Weight  146
This albhabet: e , alphabet weight: 5 , Updated String Weight  151
This albhabet:   , alphabet weight: 0 , Updated String Weight  151
This albhabet: s , alphabet weight: 19 , Updated String Weight  170
This albhabet: t , alphabet weight: 20 , Updated String Weight  190
This albhabet: r , alphabet weight: 18 , Updated String Weight  208
This albhabet: i , alphabet weight: 9 , Updated String Weight  217
This albhabet: n , alphabet weight: 14 , Updated String Weight  231
This albhabet: g , alphabet weight: 7 , Updated String Weight  238
Final String Weight:  238

Example 2: Find string weight using character weight and occurrence formula

algorithm

Step 1 - First create a file named charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 Dictionary, "f": 6, "g": 7,…………. Maximum "z": 26}

Step 2 - Now specify the given string for which the string weight is to be calculated.

Step 3 - Find the frequency of occurrence of a character in a given string.

Step 4 - Iterate over the character weight dictionary and find the weight value for each character in the given string.

Step 5 - Multiply the frequency of a character by its weight.

Step 6 - Update the string weight by adding this calculated value.

Step 7 - Repeat this and print the total result at the end.

Explanation of terms used in a given formula

TotalWeight is the total weight of the given test string.

N1, n2 represents the characters appearing in the given test string

Occr(n1) means n1 occurs in the given test string.

Weight(n1) represents the character weight of the given character n1 in the charweight dictionary.

Here ‘*’ is used as the multiplication operator for numbers

Here ‘ ’ is used as the addition operator for numbers

Formula used

TotalWeight= (Occr(n1) * Weight(n1)) (Occr(n2) * Weight(n2)) .....and so on

Example

givenstr = 'this is a sample string'
charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
WeightSum=0
occurFreq = {}
for i in givenstr:
   if i in occurFreq:
      occurFreq[i] += 1
   else:
      occurFreq[i] = 1

print("Char Weights: " , charweight)
print("Occurance: ", occurFreq)

for alphabetChar, alphabetCharCount in occurFreq.items():
   print(alphabetChar, ":", alphabetCharCount)
   for key in charweight.keys():
      if key.find(alphabetChar) > -1:
          #print(charweight[key]*alphabetCharCount)
          WeightSum=WeightSum + charweight[key]*alphabetCharCount
          #print(WeightSum)
          print("This albhabet:",alphabetChar, ", alphabet Count:", alphabetCharCount, ",  alphabet Weight:", charweight[key], " Updated String Weight ", WeightSum)

print("Final String Weight: ", WeightSum)

Output

Char Weights:  {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
Occurance:  {'t': 2, 'h': 1, 'i': 3, 's': 4, ' ': 4, 'a': 2, 'm': 1, 'p': 1, 'l': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1}
t : 2
This albhabet: t , alphabet Count: 2 ,  alphabet Weight: 20  Updated String Weight  40
h : 1
This albhabet: h , alphabet Count: 1 ,  alphabet Weight: 8  Updated String Weight  48
i : 3
This albhabet: i , alphabet Count: 3 ,  alphabet Weight: 9  Updated String Weight  75
s : 4
This albhabet: s , alphabet Count: 4 ,  alphabet Weight: 19  Updated String Weight  151
  : 4
a : 2
This albhabet: a , alphabet Count: 2 ,  alphabet Weight: 1  Updated String Weight  153
m : 1
This albhabet: m , alphabet Count: 1 ,  alphabet Weight: 13  Updated String Weight  166
p : 1
This albhabet: p , alphabet Count: 1 ,  alphabet Weight: 16  Updated String Weight  182
l : 1
This albhabet: l , alphabet Count: 1 ,  alphabet Weight: 12  Updated String Weight  194
e : 1
This albhabet: e , alphabet Count: 1 ,  alphabet Weight: 5  Updated String Weight  199
r : 1
This albhabet: r , alphabet Count: 1 ,  alphabet Weight: 18  Updated String Weight  217
n : 1
This albhabet: n , alphabet Count: 1 ,  alphabet Weight: 14  Updated String Weight  231
g : 1
This albhabet: g , alphabet Count: 1 ,  alphabet Weight: 7  Updated String Weight  238
Final String Weight:  238

in conclusion

We give here two different methods to show how to find the string weight of a given string. First, the characters used are taken from the given test string one by one and then their respective weights are added up. By repeating this process, the final string weight is calculated. In Example 2, first find the frequency of the character in the string and then multiply that frequency by the weight of that character. This process is repeated for all characters used in a given string, and the final string weight is calculated.

The above is the detailed content of Python program to find weight of string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete