1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pychu [463]
3 years ago
6

Write two methods: encrypt and decrypt. encrypt should #take as input a string, and return an encrypted version #of it according

to the rules above. # #To encrypt the string, you would: # # - Convert the string to uppercase. # - Replace all Js with Is. # - Remove all non-letter characters. # - Add an X to the end if the length if odd. # - Break the string into character pairs. # - Replace the second letter of any same-character # pair with X (e.g. LL -> LX). # - Encrypt it. # #decrypt should, in turn, take as input a string and #return the unencrypted version, just undoing the last #step. You don't need to worry about Js and Is, duplicate #letters, or odd numbers of characters in decrypt. # #For example: # # encrypt("PS. Hello, world") -> "QLGRQTVZIBTYQZ" # decrypt("QLGRQTVZIBTYQZ") -> "PSHELXOWORLDSX" # #HINT: You might find it easier if you implement some #helper functions, like a find_letter function that #returns the row and column of a letter in the cipher.
Computers and Technology
1 answer:
Harman [31]3 years ago
5 0

Answer:

The code is given below with appropriate comments

Explanation:

CIPHER = (("D", "A", "V", "I", "O"),

         ("Y", "N", "E", "R", "B"),

         ("C", "F", "G", "H", "K"),

         ("L", "M", "P", "Q", "S"),

         ("T", "U", "W", "X", "Z"))

# Add your code here!

def encrypt(plaintext):

   theList = []

   for char in plaintext:

       if char.isalpha():

           char = char.upper()

           if char == "J":

               char = "I"

           theList.append(char)

   if len(theList) % 2 == 1:

       theList.append("X")

   for i in range(0, len(theList), 2):

       if theList[i] == theList[i + 1]:

           theList[i + 1] = "X"

       findex = [-1, -1]

       sindex = [-1, -1]

       for j in range(len(CIPHER)):

           for k in range(len(CIPHER)):

               if theList[i] == CIPHER[j][k]:

                   findex = [j, k]

               if theList[i + 1] == CIPHER[j][k]:

                   sindex = [j, k]

       # same row

       if (findex[0] == sindex[0]):

           findex[1] += 1

           sindex[1] += 1

           if findex[1] == 5:

               findex[1] = 0

           if sindex[1] == 5:

               sindex[1] = 0

           theList[i] = CIPHER[findex[0]][findex[1]]

           theList[i + 1] = CIPHER[sindex[0]][sindex[1]]

       # same column

       elif (findex[1] == sindex[1]):

           findex[0] += 1

           sindex[0] += 1

           if findex[0] == 5:

               findex[0] = 0

           if sindex[0] == 5:

               sindex[0] = 0

           theList[i] = CIPHER[findex[0]][findex[1]]

           theList[i + 1] = CIPHER[sindex[0]][sindex[1]]

       else:

           theList[i] = CIPHER[findex[0]][sindex[1]]

           theList[i + 1] = CIPHER[sindex[0]][findex[1]]

   return "".join(theList)

def decrypt(ciphertext):

   theString = ""

   findex = [-1, -1]

   sindex = [-1, -1]

   for i in range(0, len(ciphertext), 2):

       for j in range(len(CIPHER)):

           for k in range(len(CIPHER)):

               if ciphertext[i] == CIPHER[j][k]:

                   findex = [j, k]

               if ciphertext[i + 1] == CIPHER[j][k]:

                   sindex = [j, k]

       if (findex[0] == sindex[0]):

           findex[1] -= 1

           sindex[1] -= 1

           if findex[1] == -1:

               findex[1] = 4

           if sindex[1] == -1:

               sindex[1] = 4

           theString += CIPHER[findex[0]][findex[1]]

           theString += CIPHER[sindex[0]][sindex[1]]

       # same column

       elif (findex[1] == sindex[1]):

           findex[0] -= 1

           sindex[0] -= 1

           if findex[0] == -1:

               findex[0] = 4

           if sindex[0] == -1:

               sindex[0] = 4

           theString += CIPHER[findex[0]][findex[1]]

           theString += CIPHER[sindex[0]][sindex[1]]

       else:

           theString += CIPHER[findex[0]][sindex[1]]

           theString += CIPHER[sindex[0]][findex[1]]

   return theString

# Below are some lines of code that will test your function.

# You can change the value of the variable(s) to test your

# function with different inputs.

#

# If your function works correctly, this will originally

# print: QLGRQTVZIBTYQZ, then PSHELXOWORLDSX

print(encrypt("PS. Hello, worlds"))

print(decrypt("QLGRQTVZIBTYQZ"))

You might be interested in
What type of media is a hard disk​
anyanavicka [17]

Answer:

A hard disk provides a high-capacity alternative to magnetic storage media. It contains metal platters coated with a magnetic layer. The platters usually spin continuously when a computer is on, storing data in different sectors on the magnetic disk.

Explanation:

5 0
3 years ago
What does the phrase at the drop of a hat mean?
umka2103 [35]

Answer:

When you use the expression 'At the Drop of a Hat' you mean that something is happening instantly, without any delay. Example of use: “We're all packed and ready to go; we can leave at the drop of a hat.”

Explanation:

Hope this helped!!  :)

Pls thank and maybe brainliest??

5 0
3 years ago
Read 2 more answers
Create a poem about MS powerpoint presentation
iren2701 [21]
You could use poem generator online or see samples and change some words
5 0
3 years ago
What is the best free game designing program for windows
Nadusha1986 [10]
Python, Unity 3D, C++Notepad
8 0
3 years ago
Asking yourself questions can help you think of what to _____, in order to get answers.
Olenka [21]
Ask your self questions can help you think of what to measure, in order to get answers.
8 0
3 years ago
Other questions:
  • Write a program to input money in cents from user, example 12745 and display the one dollar bills and the cents. Submit source c
    12·1 answer
  • im in honors comupter science and one of my questions is evaluate 18% 7 does anyone know how to answer that
    6·1 answer
  • What is variable declaration in java ​
    6·2 answers
  • If we collect data on the number of wins each team in the NFL had during the 2011-12 season, we have _____________ data.
    10·1 answer
  • The area of a rectangle is the rectangle's length times its width.
    5·1 answer
  • Convert hexadecimal number 1AF2 to a decimal number.
    7·1 answer
  • What is E-Commerce? Answer The Following. Pls Help Me​
    5·1 answer
  • Write a program that inputs a five-digit integer, spearates the integer into its digits and prints them seperated by three space
    6·1 answer
  • Jason works for a restaurant that serves only organic, local produce. What
    15·2 answers
  • PACIFIC NORTHWEST
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!