Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: UTF-8 -*-
- # Filename: base_i_.py
- # Author: Jeoi Reqi
- """
- 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.
- Requirements:
- - Python 3
- Usage:
- 1. Ensure You Have Python 3 Installed On Your System.
- 2. Run The Script To Either Encode or Decode.
- 3. If Encoding Or Decoding, Provide The Full Character Set (CS) & The Base-10 Encoded String.
- 4. Using Assigned Numerical Values, The Script Will Encode Or Decode String And Display The Output.
- # EXAMPLE DATA
- [CS]: Character List Example: ('i' Number Of Characters + [SPACE])
- '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' # CHANGE TO ANY CHARACTER SET YOU WANT TO ENCODE USING.
- [Encoded]: Base-i String to Base-10:
- 'YOUR ENCODED STRING IN BASE-10 WOULD SHOW HERE'
- [Decoded]: Base-10 String to Base-i:
- 'DECODED STRING SHOWS HERE'
- """
- def assign_numbers(characters):
- return {char: str(index).zfill(3) for index, char in enumerate(characters)}
- def encode_basei(input_string, encoding_dict):
- try:
- result_values = [encoding_dict[char] for char in input_string]
- result_string = ''.join(result_values)
- return result_string
- except KeyError:
- return "Invalid Characters In Input String For Encoding"
- def decode_basei(input_string, decoding_dict):
- try:
- base10_values = [input_string[i:i+3] for i in range(0, len(input_string), 3)]
- result_chars = [decoding_dict[val] for val in base10_values]
- result_string = ''.join(result_chars)
- return result_string
- except KeyError:
- return "Invalid Base-10 Values In Input String"
- # Option to Encode or Decode
- operation = input("\nSelect Your Method:\n1: Encode\n2: Decode\nEnter Your Choice (1 Or 2):").lower()
- if operation == '1':
- user_input = (' ' + input("Enter the full character set (CS): ")) # Add a [SPACE] before the user input string
- assigned_numbers = assign_numbers(user_input + ' ')
- user_string = ' ' + input("Enter the string to encode: ")
- encoded_base10 = encode_basei(user_string, assigned_numbers)
- print("\n[Encoded]:\tBase-i String to Base-10:\n\n", encoded_base10, "\n")
- elif operation == '2':
- user_input = input("Enter the full character set (CS): ")
- assigned_numbers = assign_numbers(user_input + ' ')
- user_string = input("Enter the Base-10 string to decode: ")
- inverted_numbers = {v: k for k, v in assigned_numbers.items()}
- decoded_basei = decode_basei(user_string, inverted_numbers)
- print("\n[Decoded]:\tBase-10 String to Base-i:\n\n", decoded_basei, "\n")
- else:
- print("Invalid operation. Please enter '1' to encode or '2' to decode.")
- # Testing Functions Below: (DO NOT REMOVE!)
- """
- # Print Function To Verify Decoding Matches The Original Character Set
- print("\n[Verify]:\tOriginal Base-i Character Set:\n\n", [CS], "\n")
- # Print The Assigned Numbers With Their Characters in a Grid (20 per line)
- print("\nAssigned Numbers With Characters:")
- for i, (char, number) in enumerate(assigned_numbers.items(), start=1):
- print(f"{number}: {char}\t", end='' if i % 20 != 0 else '\n')
- ## END OF TESTS ##
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement