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
In a process called _____, excel continually reviews the workbook for errors in formulas as you create or manipulate it.
Nataliya [291]
Background formula checking is the process in excel that enables it to review continually the whole file for formula errors. This also functions while you are creating and entering new formulas in cells. When an error is found certain codes shows on the cells.
3 0
3 years ago
A typical analog cell phone has a frequency of 850 mhz; a digital phone a frequency of 1950 mhz. compared to the signal from an
Zepler [3.9K]
A higher frequency than the analog cell phone.
5 0
3 years ago
6
Novosadov [1.4K]

Answer:

government and New Zealand

Explanation:

you can see the web site name the name is gov.nz ,gov mens- government and nz means new Zealand

thank you like us

8 0
2 years ago
Read 2 more answers
What are the four different orchestral instrument families?
natka813 [3]

Answer:

Gutair

Violen

cello

Double Brass

Harp

Explanation:

7 0
3 years ago
Read 2 more answers
If you had an idea for a new software company, what would be the best approach to help make it a successful business? develop a
Klio2033 [76]

Answer:

develop a business plan to describe how to maintain and grow revenues

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • Determine the number of bytes necessary to store an uncompressed binary image of size 4000 × 3000 pixels.
    9·1 answer
  • Prove that for every nfa with an arbitrary number of final states there is an equivalent nfa with only one final state. Can we m
    8·1 answer
  • All objects in an object-oriented program are instantiated (created) from a ____.
    12·1 answer
  • What does the presence of the cydia app on an ios device indicate?
    14·1 answer
  • A local government uses Short Message Service (SMS) text messages to alert local residents when roads are closed. Which of the f
    13·1 answer
  • Write a method that determines the total number of chars in each string of an array.
    12·1 answer
  • All portions, to include subject, title, paragraphs, sub-paragraphs, graphics, tables, charts, and bullet statements, must be pr
    12·1 answer
  • How would you design an adaptive environment for people who are blind?
    10·1 answer
  • Area Triangolo Rettangolo in c++
    6·1 answer
  • Help me with this two questions please
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!