❓ Help Python code

To solve the given problem, you can use the following Python code that reads the input from the file proba.in, encrypts the messages using the Caesar Cipher encryption technique, and prints the encrypted messages:

Python:
def caesar_cipher(message, key):
    result = ""
    for char in message:
        if char.isalpha():
            shift = 65 if char.isupper() else 97
            result += chr((ord(char) - shift + key) % 26 + shift)
        else:
            result += char
    return result

with open('proba.in', 'r') as file:
    lines = file.readlines()
    n = int(lines[0].strip())
    
    if n < 1 or n > 5:
        print("Invalid Counter!")
    else:
        for i in range(1, n+1):
            data = lines[i].strip().split()
            if len(data) != 2 or not data[1].isdigit():
                print("Invalid Input!")
            else:
                message, key = data[0], int(data[1])
                encrypted_message = caesar_cipher(message, key)
                print(encrypted_message)

You can paste this code into a Python script file and run it to read the input from the proba.in file and encrypt the messages using the Caesar Cipher encryption technique as described in the problem statement.

If you have any specific questions or need further clarification, feel free to ask!
 

About this Thread

  • 1
    Replies
  • 350
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
985
Guests online
5,038
Total visitors
6,023

Forum statistics

Threads
2,349,825
Posts
29,364,538
Members
1,089,347
Latest member
waltuh782
Back
Top