Advertisement
Python253

string_pack_unpack

May 13th, 2024
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: string_pack_unpack.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script provides functions for packing and unpacking strings, allowing users to compress and decompress text data efficiently.
  9.  
  10. Functions:
  11.    - pack_string(string):
  12.      Compresses the input string by encoding repeated characters as (count, character) tuples.
  13.    - unpack_string(compressed_string):
  14.      Decompresses the compressed string by expanding the (count, character) tuples back into the original string.
  15.  
  16. Requirements:
  17.    - This script requires Python 3.x.
  18.  
  19. Usage:
  20.    - Import the functions from this script into your Python code.
  21.    - Call the pack_string function with the string you want to compress to obtain the compressed version.
  22.    - Call the unpack_string function with the compressed string to obtain the original uncompressed string.
  23.  
  24. Compression Example:
  25.    Options Menu
  26.  
  27.    1: Compress
  28.    2: Decompress
  29.  
  30.    Make your selection (1 or 2): 1
  31.  
  32.    Enter string to compress: A Bb Ccc
  33.  
  34.    Compressed string: [(1, 'A'), (1, ' '), (1, 'B'), (1, 'b'), (1, ' '), (1, 'C'), (2, 'c')]
  35.  
  36. Decompression Example:
  37.    Options Menu
  38.  
  39.    1: Compress
  40.    2: Decompress
  41.  
  42.    Make your selection (1 or 2): 2
  43.  
  44.    Enter compressed string: [(1, 'A'), (1, ' '), (1, 'B'), (1, 'b'), (1, ' '), (1, 'C'), (2, 'c')]
  45.  
  46.    Decompressed string: A Bb Ccc
  47. """
  48.  
  49.  
  50. from itertools import groupby
  51.  
  52. def compress():
  53.     """
  54.    Compresses the input string by encoding repeated characters as (count, character) tuples.
  55.    """
  56.     uncompressed = str(input("\nEnter string to compress: "))  # user input for string
  57.     compressed = [(len(list(value)), str(key)) for key, value in groupby(uncompressed)]
  58.     print("\nCompressed string:", compressed)  # Print the compressed string
  59.  
  60. def decompress():
  61.     """
  62.    Decompresses the compressed string by expanding the (count, character) tuples back into the original string.
  63.    """
  64.     compressed = input("\nEnter compressed string: ")  # Get the compressed string from the user
  65.     decompressed = ""  # Initialize the decompressed string
  66.  
  67.     # Split the compressed string into tuples of (count, character) and iterate through them
  68.     pairs = compressed.strip('[]').split('), (')  # Adjust the splitting logic to handle the comma and space
  69.     for pair in pairs:
  70.         count, char = pair.strip('()').split(", ")  # Split each pair into count and character
  71.         count = int(count)  # Convert count to integer
  72.         char = char.strip("'")  # Remove single quotes around the character
  73.         decompressed += char * count  # Append the character 'count' times to the decompressed string
  74.  
  75.     print("\nDecompressed string:", decompressed)  # Print the decompressed string
  76.  
  77. # Options menu
  78. print("\nOptions Menu\n\n1: Compress\n2: Decompress\n")
  79. selection = input("Make your selection (1 or 2): ")
  80.  
  81. if selection == '1':
  82.     compress()
  83. elif selection == '2':
  84.     decompress()
  85. else:
  86.     print("\nInvalid selection!\n")
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement