Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find the string weight
In this article, we will calculate the total weight of a string by assigning numeric values to each character. The weight system is: a=1, b=2, c=3, and so on up to z=26. We'll explore two different approaches to find the string weight.
Method 1: Direct Character Weight Addition
This approach iterates through each character in the string and adds its corresponding weight to a running total.
Algorithm
Step 1 Create a reference string with space and all lowercase letters.
Step 2 Use the index() function to get weight values (space = 0, a = 1, b = 2, etc.).
Step 3 Convert the input string to lowercase for consistency.
Step 4 Iterate through each character in the string.
Step 5 Find the position (weight) of each character.
Step 6 Add the weight to the running total.
Example
def calculate_weight_method1(text):
text = text.lower()
reference = ' abcdefghijklmnopqrstuvwxyz'
total_weight = 0
for char in text:
char_weight = reference.index(char)
total_weight += char_weight
print(f"Character: '{char}', Weight: {char_weight}, Total: {total_weight}")
return total_weight
# Test the function
test_string = 'hello'
result = calculate_weight_method1(test_string)
print(f"Final String Weight: {result}")
Character: 'h', Weight: 8, Total: 8 Character: 'e', Weight: 5, Total: 13 Character: 'l', Weight: 12, Total: 25 Character: 'l', Weight: 12, Total: 37 Character: 'o', Weight: 15, Total: 52 Final String Weight: 52
Method 2: Frequency-Based Weight Calculation
This approach first counts the frequency of each character, then multiplies frequency by weight for efficiency.
Formula
Total Weight = (Frequency(char1) × Weight(char1)) + (Frequency(char2) × Weight(char2)) + ...
Algorithm
Step 1 Create a dictionary mapping each letter to its weight.
Step 2 Count the frequency of each character in the string.
Step 3 For each unique character, multiply its frequency by its weight.
Step 4 Sum all the weighted values.
Example
def calculate_weight_method2(text):
# Create weight dictionary
char_weights = {chr(i): i - 96 for i in range(97, 123)} # a=1, b=2, ..., z=26
# Count character frequencies
frequency = {}
for char in text.lower():
if char.isalpha(): # Only count letters
frequency[char] = frequency.get(char, 0) + 1
print("Character frequencies:", frequency)
total_weight = 0
for char, count in frequency.items():
weight = char_weights[char]
contribution = count * weight
total_weight += contribution
print(f"'{char}': {count} × {weight} = {contribution}")
return total_weight
# Test the function
test_string = 'hello'
result = calculate_weight_method2(test_string)
print(f"Final String Weight: {result}")
Character frequencies: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
'h': 1 × 8 = 8
'e': 1 × 5 = 5
'l': 2 × 12 = 24
'o': 1 × 15 = 15
Final String Weight: 52
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Direct Addition | O(n) | O(1) | Simple implementation |
| Frequency-Based | O(n) | O(k) where k = unique chars | Analyzing character distribution |
Conclusion
Both methods calculate string weight effectively. Use the direct addition method for simplicity, or the frequency-based method when you need character distribution analysis. The frequency method is more efficient for strings with many repeated characters.
