diff --git a/Projects/Message_Encryption.py b/Projects/Message_Encryption.py new file mode 100644 index 000000000000..822de1b2275d --- /dev/null +++ b/Projects/Message_Encryption.py @@ -0,0 +1,69 @@ +import random as ra +import string as str + + +def options(): + while True: + print("========== Welcome To Message Encryption ==========") + print("\t'1' For Encryption") + print("\t'2' For Decode") + try: + opt = int(input("Enter: ")) + if opt not in ("1", "2", 1, 2): + print("Choose Number Between '1','2'") + if opt == 1: + encrypt() + elif opt == 2: + decode() + except ValueError: + print("Enter Numbers Only") + + +def encrypt(): + x = input("Enter Your Message :") + + if len(x) <= 3: + encrypt_key = input("Enter Your Key '1','4','7' :") + + with open("User_Key.txt", "a+") as f: + f.write(f"{encrypt_key},{x}\n") + print(f"Successfully Encrypted: {x[::-1]}") + + encrypt_key = input("Enter Your Key '1','4','7' :") + + with open("User_Key.txt", "a") as f: + f.write(f"{encrypt_key},{x}\n") + shift = x[1:] + x[0] + prefix = "".join(ra.choice(str.ascii_letters) for _ in range(3)) + suffix = "".join(ra.choice(str.ascii_letters) for _ in range(3)) + message = prefix + shift + suffix + message = message.strip() + print(f"Successfully Encrypted: {message}") + return encrypt_key + + +def decode(): + with open("User_Key.txt", "r") as f: + user_message = input("Enter Message To Decode : ") + decode_key = input("Enter Your Key : ") + found = False + + for line in f: + encrypt_key, x = line.strip().split(",") + + if encrypt_key == decode_key: + if len(user_message) <= 3: + print(user_message[::-1]) + else: + x = user_message[3:-3] + x = x[-1] + x[:-1] + print(f"Message Decoded : {x}") + + found = True + break + + if not found: + print("Didn't Match :/") + + +options() diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index b75491d9a949..fba63280c59f 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -54,7 +54,9 @@ def mutate(child: str, genes: list[str]) -> str: """ child_list = list(child) if random.uniform(0, 1) < MUTATION_PROBABILITY: - child_list[random.randint(0, len(child)) - 1] = random.choice(genes) + random_index = random.randint(0, len(child) - 1) + child_list[random_index] = random.choice(genes) + return "".join(child_list) @@ -175,7 +177,9 @@ def basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int, # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. - population_best = population[: int(N_POPULATION / 3)] + population_best = [ + item[0] for item in population_score[: int(N_POPULATION / 3)] + ] population.clear() population.extend(population_best) # Normalize population score to be between 0 and 1.