Python: Hash a word
Python Basic: Exercise-74 with Solution
Write a Python program to hash a word.
Sample Solution-1:
Python Code:
# Create a list that maps characters to their Soundex codes.
soundex = [0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2]
# Prompt the user to input the word to be hashed.
word = input("Input the word to be hashed: ")
# Convert the input word to uppercase.
word = word.upper()
# Initialize the coded word with the first character of the input word.
coded = word[0]
# Iterate over the remaining characters in the word to calculate the Soundex code.
for a in word[1:len(word)]:
# Calculate the index for the Soundex list based on the character's ASCII code.
i = 65 - ord(a)
coded = coded + str(soundex[i])
# Print a blank line.
print()
# Display the coded word.
print("The coded word is: " + coded)
# Print a blank line.
print()
Sample Output:
Input the word be hashed: w3r The coded word is: W02
Flowchart:
Python Code Editor:
Previous: Write a Python program to calculate midpoints of a line.
Next: Write a Python program to get the copyright information and write Copyright information in Python code.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/python-basic-exercise-74.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics