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
Selected Reading
Zip function in Python to change to a new character set.
The zip() function in Python can be used to create a mapping between two character sets. This is useful when you want to translate text from one character encoding to another, such as converting from a custom character set back to the standard alphabet.
Problem Statement
Given a custom 26-letter character set and the standard alphabet, we need to create a mapping to translate strings from the custom set back to normal letters.
Custom character set: qwertyuiopasdfghjklzxcvbnm Standard alphabet: abcdefghijklmnopqrstuvwxyz Input: "wwmm" Output: "bbzy"
Algorithm
- Define the custom character set and standard alphabet
- Use
zip()to create pairs between custom and standard characters - Convert the pairs into a dictionary for quick lookup
- Iterate through input string and map each character
- Join the mapped characters to form the output string
Implementation
def newString(custom_set, input_string):
standard_alphabet = 'abcdefghijklmnopqrstuvwxyz'
# Create mapping from custom character set to standard alphabet
char_mapping = dict(zip(custom_set, standard_alphabet))
# Map each character in input string
mapped_chars = [char_mapping[char] for char in input_string]
result = ''.join(mapped_chars)
return result
# Test the function
custom_char_set = 'qwertyuiopasdfghjklzxcvbnm'
input_text = 'wwmm'
output = newString(custom_char_set, input_text)
print(f"Input: {input_text}")
print(f"Output: {output}")
Input: wwmm Output: bbzy
How It Works
The zip() function pairs corresponding characters from both strings:
custom = 'qwertyuiopasdfghjklzxcvbnm'
standard = 'abcdefghijklmnopqrstuvwxyz'
# Show first few mappings
mapping = dict(zip(custom, standard))
print("First 5 character mappings:")
for i, (custom_char, standard_char) in enumerate(zip(custom, standard)):
if i < 5:
print(f"'{custom_char}' ? '{standard_char}'")
First 5 character mappings: 'q' ? 'a' 'w' ? 'b' 'e' ? 'c' 'r' ? 'd' 't' ? 'e'
Complete Example with Validation
def translate_string(custom_set, input_string):
standard_alphabet = 'abcdefghijklmnopqrstuvwxyz'
# Validate input
if len(custom_set) != 26:
return "Error: Custom character set must have exactly 26 characters"
# Create character mapping
char_mapping = dict(zip(custom_set, standard_alphabet))
# Translate each character
try:
result = ''.join(char_mapping[char] for char in input_string.lower())
return result
except KeyError as e:
return f"Error: Character {e} not found in custom set"
# Test with different inputs
custom_set = 'qwertyuiopasdfghjklzxcvbnm'
test_cases = ['wwmm', 'qwerty', 'hello']
for test in test_cases:
result = translate_string(custom_set, test)
print(f"'{test}' ? '{result}'")
'wwmm' ? 'bbzy' 'qwerty' ? 'abcdef' 'hello' ? 'dcjjg'
Conclusion
The zip() function provides an elegant way to create character mappings between different encoding schemes. This technique is useful for simple substitution ciphers and character set translations.
Advertisements
