❓ 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
  • 319
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,104
Guests online
1,304
Total visitors
2,408

Forum statistics

Threads
2,285,527
Posts
29,030,978
Members
1,218,503
Latest member
elleu
Back
Top