Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- # FUNCTIONS
- def apply_substitution(cyphered_text, substitution_table, highlighted_char):
- """Applies a substitution table to a cyphered text, with colored output."""
- converted_text = ""
- for char in cyphered_text:
- if char == highlighted_char:
- converted_text += f"\033[0;33m{char}\033[0m"
- elif char in substitution_table:
- converted_text += f"\033[92m{substitution_table[char]}\033[0m" # Green color
- else:
- converted_text += char
- return converted_text
- def apply_highlight(text, highlightedchar):
- """highlights a character in a text"""
- highlighted_text = ""
- for char in text:
- if char == highlightedchar:
- highlighted_text += f"\033[0;33m{char}\033[0m"
- else:
- highlighted_text += char
- return highlighted_text
- #PROGRAM
- cyphered_message = """
- 1234567/8393068/3019A8/560037A9C8112C\\4A1587E
- 92A95H450L/F
- C2AM34A9R23/923031A679
- 5H5609CT3/2319C77
- 3VA19CT3
- 6897
- 92CO9E
- """
- substitution_table = {}
- highlighted_char = ""
- last_action = ""
- while True:
- os.system('cls')
- print("\n---")
- print("Cyphered Message:")
- print(apply_highlight(cyphered_message, highlighted_char))
- print("\n---")
- print("Converted Message:")
- converted_message = apply_substitution(cyphered_message, substitution_table, highlighted_char)
- print(converted_message)
- print("---")
- print("\033[0;36m" + last_action + "\033[0m")
- print("\nActions:")
- print("add-> A [original] [substitute]")
- print("remove-> R [original]")
- print("count-> C [original]")
- print("highlight-> H [original]")
- print("clear-> CLR")
- print("quit-> Q")
- action = input("Enter an action (A[DD], R[EM], C[OUNT], H[IGHLIGHT], CLR, Q[UIT]): ").upper().split()
- if not action:
- continue
- if action[0] == 'A' or action[0] == 'ADD':
- if len(action) == 3:
- original_char = action[1]
- substitute_char = action[2]
- if len(original_char) == 1 and len(substitute_char) == 1:
- substitution_table[original_char] = substitute_char
- last_action= f"Added substitution: '{original_char}' -> '{substitute_char}'"
- else:
- last_action = "Invalid input. Both original and substitute must be single characters."
- else:
- last_action= "Usage: A[DD] [original_char] [substitute_char]"
- elif action[0] == 'QUIT' or action[0] == 'Q':
- print("Exiting.")
- break
- elif action[0] == 'CLR':
- substitution_table.clear()
- last_action = "Substitution table cleared."
- elif action[0] == 'R' or action[0] == 'REM':
- if len(action) == 2:
- original_char = action[1]
- if original_char in substitution_table:
- del substitution_table[original_char]
- last_action= f"Removed substitution for '{original_char}'."
- else:
- last_action= f"No substitution found for '{original_char}'."
- else:
- last_action= "Usage: R[EM] [original_char]"
- elif action[0] == 'C' or action[0] == 'COUNT':
- if len(action) == 2:
- original_char = action[1]
- count = cyphered_message.count(original_char)
- last_action = f"Occurrences of {original_char}': {count}"
- else:
- last_action= "Usage: C[OUNT] [original_char]"
- elif action[0] == 'H' or action[0] == 'HIGHLIGHT':
- if len(action) == 2:
- highlighted_char = action[1]
- last_action = f"Highlighted '{highlighted_char}'."
- else:
- last_action="Invalid action. Use 'A[DD]', 'R[EM]', 'C[OUNT]', 'H[IGHLIGHT]', 'CLR', or 'Q[UIT]'."
Advertisement
Add Comment
Please, Sign In to add comment