Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: string_pack_unpack.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script provides functions for packing and unpacking strings, allowing users to compress and decompress text data efficiently.
- Functions:
- - pack_string(string):
- Compresses the input string by encoding repeated characters as (count, character) tuples.
- - unpack_string(compressed_string):
- Decompresses the compressed string by expanding the (count, character) tuples back into the original string.
- Requirements:
- - This script requires Python 3.x.
- Usage:
- - Import the functions from this script into your Python code.
- - Call the pack_string function with the string you want to compress to obtain the compressed version.
- - Call the unpack_string function with the compressed string to obtain the original uncompressed string.
- Compression Example:
- Options Menu
- 1: Compress
- 2: Decompress
- Make your selection (1 or 2): 1
- Enter string to compress: A Bb Ccc
- Compressed string: [(1, 'A'), (1, ' '), (1, 'B'), (1, 'b'), (1, ' '), (1, 'C'), (2, 'c')]
- Decompression Example:
- Options Menu
- 1: Compress
- 2: Decompress
- Make your selection (1 or 2): 2
- Enter compressed string: [(1, 'A'), (1, ' '), (1, 'B'), (1, 'b'), (1, ' '), (1, 'C'), (2, 'c')]
- Decompressed string: A Bb Ccc
- """
- from itertools import groupby
- def compress():
- """
- Compresses the input string by encoding repeated characters as (count, character) tuples.
- """
- uncompressed = str(input("\nEnter string to compress: ")) # user input for string
- compressed = [(len(list(value)), str(key)) for key, value in groupby(uncompressed)]
- print("\nCompressed string:", compressed) # Print the compressed string
- def decompress():
- """
- Decompresses the compressed string by expanding the (count, character) tuples back into the original string.
- """
- compressed = input("\nEnter compressed string: ") # Get the compressed string from the user
- decompressed = "" # Initialize the decompressed string
- # Split the compressed string into tuples of (count, character) and iterate through them
- pairs = compressed.strip('[]').split('), (') # Adjust the splitting logic to handle the comma and space
- for pair in pairs:
- count, char = pair.strip('()').split(", ") # Split each pair into count and character
- count = int(count) # Convert count to integer
- char = char.strip("'") # Remove single quotes around the character
- decompressed += char * count # Append the character 'count' times to the decompressed string
- print("\nDecompressed string:", decompressed) # Print the decompressed string
- # Options menu
- print("\nOptions Menu\n\n1: Compress\n2: Decompress\n")
- selection = input("Make your selection (1 or 2): ")
- if selection == '1':
- compress()
- elif selection == '2':
- decompress()
- else:
- print("\nInvalid selection!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement