Advertisement
Python253

base_i

Mar 7th, 2024
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # Filename: base_i_.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This Python Script Encodes Or Decodes The User Input String Using The Full User Character Set ('i'+ [SPACE]) to Base-10, Using Assigned Numerical Values & Outputs The Results.
  8.  
  9. Requirements:
  10. - Python 3
  11.  
  12. Usage:
  13. 1. Ensure You Have Python 3 Installed On Your System.
  14. 2. Run The Script To Either Encode or Decode.
  15. 3. If Encoding Or Decoding, Provide The Full Character Set (CS) & The Base-10 Encoded String.
  16. 4. Using Assigned Numerical Values, The Script Will Encode Or Decode String And Display The Output.
  17.  
  18. # EXAMPLE DATA
  19.  
  20. [CS]: Character List Example: ('i' Number Of Characters + [SPACE])
  21.                '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' # CHANGE TO ANY CHARACTER SET YOU WANT TO ENCODE USING.
  22.  
  23. [Encoded]: Base-i String to Base-10:
  24. 'YOUR ENCODED STRING IN BASE-10 WOULD SHOW HERE'
  25.  
  26. [Decoded]: Base-10 String to Base-i:
  27. 'DECODED STRING SHOWS HERE'
  28. """
  29.  
  30. def assign_numbers(characters):
  31.     return {char: str(index).zfill(3) for index, char in enumerate(characters)}
  32.  
  33. def encode_basei(input_string, encoding_dict):
  34.     try:
  35.         result_values = [encoding_dict[char] for char in input_string]
  36.         result_string = ''.join(result_values)
  37.         return result_string
  38.     except KeyError:
  39.         return "Invalid Characters In Input String For Encoding"
  40.  
  41. def decode_basei(input_string, decoding_dict):
  42.     try:
  43.         base10_values = [input_string[i:i+3] for i in range(0, len(input_string), 3)]
  44.         result_chars = [decoding_dict[val] for val in base10_values]
  45.         result_string = ''.join(result_chars)
  46.         return result_string
  47.     except KeyError:
  48.         return "Invalid Base-10 Values In Input String"
  49.  
  50. # Option to Encode or Decode
  51. operation = input("\nSelect Your Method:\n1: Encode\n2: Decode\nEnter Your Choice (1 Or 2):").lower()
  52.  
  53. if operation == '1':
  54.     user_input = (' ' + input("Enter the full character set (CS): "))  # Add a [SPACE] before the user input string
  55.     assigned_numbers = assign_numbers(user_input + ' ')
  56.     user_string = ' ' + input("Enter the string to encode: ")
  57.     encoded_base10 = encode_basei(user_string, assigned_numbers)
  58.     print("\n[Encoded]:\tBase-i String to Base-10:\n\n", encoded_base10, "\n")
  59.  
  60. elif operation == '2':
  61.     user_input = input("Enter the full character set (CS): ")
  62.     assigned_numbers = assign_numbers(user_input + ' ')
  63.     user_string = input("Enter the Base-10 string to decode: ")
  64.     inverted_numbers = {v: k for k, v in assigned_numbers.items()}
  65.     decoded_basei = decode_basei(user_string, inverted_numbers)
  66.     print("\n[Decoded]:\tBase-10 String to Base-i:\n\n", decoded_basei, "\n")
  67.  
  68. else:
  69.     print("Invalid operation. Please enter '1' to encode or '2' to decode.")
  70.  
  71.  
  72.  
  73. # Testing Functions Below: (DO NOT REMOVE!)
  74.  
  75. """
  76. # Print Function To Verify Decoding Matches The Original Character Set
  77. print("\n[Verify]:\tOriginal Base-i Character Set:\n\n", [CS], "\n")
  78.  
  79. # Print The Assigned Numbers With Their Characters in a Grid (20 per line)
  80. print("\nAssigned Numbers With Characters:")
  81. for i, (char, number) in enumerate(assigned_numbers.items(), start=1):
  82.    print(f"{number}: {char}\t", end='' if i % 20 != 0 else '\n')
  83.  
  84.  
  85.  
  86. ## END OF TESTS ##
  87. """
  88.    
  89.    
  90.    
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement