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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Notepad++ to run a Python program requires the following steps: 1. Install the Python plug-in; 2. Create a Python file; 3. Set the run options; 4. Run the program.

Llama3 is here! Just now, Meta’s official website was updated and the official announced Llama 38 billion and 70 billion parameter versions. And it is an open source SOTA after its launch: Meta official data shows that the Llama38B and 70B versions surpass all opponents in their respective parameter scales. The 8B model outperforms Gemma7B and Mistral7BInstruct on many benchmarks such as MMLU, GPQA, and HumanEval. The 70B model has surpassed the popular closed-source fried chicken Claude3Sonnet, and has gone back and forth with Google's GeminiPro1.5. As soon as the Huggingface link came out, the open source community became excited again. The sharp-eyed blind students also discovered immediately

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

The Python program development process includes the following steps: Requirements analysis: clarify business needs and project goals. Design: Determine architecture and data structures, draw flowcharts or use design patterns. Writing code: Program in Python, following coding conventions and documentation comments. Testing: Writing unit and integration tests, conducting manual testing. Review and Refactor: Review code to find flaws and improve readability. Deploy: Deploy the code to the target environment. Maintenance: Fix bugs, improve functionality, and monitor updates.

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s
