answer.
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]
1 year 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]1 year 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
Mobile computing has two major characteristics that differentiate it from other forms of computing. What are these two character
olya-2409 [2.1K]

Answer:

mobility and broad reach  

Explanation:

The two major characteristics of mobile computing are:

Mobility: It basically refers to the portability. Mobility means that users can carry their mobile device wherever they go. This facilitates real-time communication with other devices. Users can take a mobile device anywhere and can contact with other devices and systems via a wireless network for example a user, wherever he happens to be, can log in to his email account to check his emails using his mobile phone.

Broad Reach: It basically means that the mobile device users can be reached at any time. This means that the users of an open mobile device can be instantaneously contacted. Having an open mobile device means that it can be reached by or connected to other mobile networks. However mobile users also have options to restrict specific messages or calls.

6 0
2 years ago
Rupa would like to quickly insert a table into her document without having to worry about formatting the data in the table. Whic
USPshnik [31]
Internet tap and log on application
6 0
1 year ago
Read 2 more answers
1- Design a brute-force algorithm for solving the problem below (provide pseudocode): You have a large container with storage si
sladkih [1.3K]

Answer:

Pseudocode is as follows:

// below is a function that takes two parameters:1. An array of items 2. An integer for weight W

// it returns an array of selected items which satisfy the given condition of sum <= max sum.

function findSubset( array items[], integer W)

{

initialize:

maxSum = 0;

ansArray = [];

// take each "item" from array to create all possible combinations of arrays by comparing with "W" and // "maxSum"

start the loop:

// include item in the ansArray[]

ansArray.push(item);

// remove the item from the items[]

items.pop(item);

ansArray.push(item1);

start the while loop(sum(ansArray[]) <= W):

// exclude the element already included and start including till

if (sum(ansArray[]) > maxSum)

// if true then include item in ansArray[]

ansArray.push(item);

// update the maxSum

maxSum = sum(ansArray[items]);

else

// move to next element

continue;

end the loop;

// again make the item[] same by pushing the popped element

items.push(item);

end the loop;

return the ansArray[]

}

Explanation:

You can find example to implement the algorithm.

3 0
1 year ago
Write the definition of a function printAttitude, which has an int parameter and returns nothing. The function prints a message
Oksana_A [137]

Answer:

The function definition, in cpp, for the function printAttitude is given below.

void printAttitude(int n)

{

switch(n)

{

case 1: cout<<"disagree"<<endl; break;

case 2: cout<<"no opinion"<<endl; break;

case 3: cout<<"agree"<<endl; break;

default: break;

}

}

Explanation:

As seen, the method takes an integer parameter.

The method does not returns any value hence, return type is void.

The message is displayed to the console using switch statement which executes on the integer parameter, n.

The values for which output needs to be displayed are taken in cases.

The values for which no output needs to be displayed, is taken in default case.

Every case ends with break statement so that the program execution can be terminated.

This method can be used inside a program as shown.  

PROGRAM

#include <iostream>

using namespace std;

void printAttitude(int n)

{

switch(n)

{

case 1: cout<<"disagree"<<endl; break;

case 2: cout<<"no opinion"<<endl; break;

case 3: cout<<"agree"<<endl; break;

default: break;

}

}

int main() {

// variables to hold respective value

int n;

// user input taken for n

cout<<"Enter any number: ";

cin>>n;

// calling the function taking integer parameter  

printAttitude(n);

return 0;

}

OUTPUT

Enter any number: 11

1. The user input is taken for the integer parameter.

2. Inside main(), the method, printAttitude(), is then called and the user input is passed as a parameter.

3. Since the user entered value is 11 which does not satisfies any of the values in the cases, the default case is entered which does nothing and executes the break statement.

4. Hence, the program displays nothing for the value of 11.

5. When the user enters value 3, the case statement is executed since 3 satisfies one of the case values.

6. The program output for the user input of 3 is as shown below.

OUTPUT

Enter any number: 3

agree

The program ends with return statement.

4 0
1 year ago
The following code should take a number as input, multiply it by 8, and print the result. In line 2 of the code below, the * sym
Juli2301 [7.4K]

Answer:

num = int(input("enter a number:"))

print(num * 8)

Explanation:

num is just a variable could be named anything you want.

if code was like this num = input("enter a number:")

and do a print(num * 8)

we get an error because whatever the user puts in input comes out a string.

we cast int() around our input() function to convert from string to integer.

therefore: num = int(input("enter a number:"))

will allow us to do  print(num * 8)

6 0
2 years ago
Other questions:
  • Social networking sites like Office Online, PayPal, and Dropbox are used to develop social and business contacts.
    6·2 answers
  • Where does an MPLS label go in a PDU?
    13·1 answer
  • What is the first step to apply the line and page breaks options to groups of paragraphs in a Word document?
    8·2 answers
  • Escribe, en los siguientes cuadros, los conceptos que correspondan
    10·1 answer
  • A computer system uses passwords that are six characters and each character is one of the 26 letters (a-z) or 10 integers (0-9).
    13·1 answer
  • Warm colors including red, yellow and orange _____________ be energetic and exciting to the eye, while cool colors in blue, gree
    11·1 answer
  • Se dau lungimile a N cuvinte (0 &lt; N ≤ 5 000), formate din cel puțin un caracter. Să se afișeze lungimea minimă necesară R a u
    14·1 answer
  • Determining the Services Running on a Network Alexander Rocco Corporation has multiple OSs running in its many branch offices. B
    10·1 answer
  • The web development team is having difficulty connecting by ssh to your local web server, and you notice the proper rule is miss
    8·1 answer
  • Arrays are described as immutable because they are two dimensional. are arranged sequentially. can be reordered. cannot be chang
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!