Question

In python write a function

In python write a function

Homework Answers

Answer #1

How the Encryption is done:

In the program, first a grid is made, where number of rows is given and number of columns is such that it can store the whole 'plaintext' (There can be extra spaces left in the grid other than 'plaintext'). Number of columns is calculated using ceil function.

The plaintext is stored in the grid in following manner.

1st letter of plaintext 10th letter 11th letter
2nd letter 9th letter last letter
3rd letter 8th letter
4th letter 7th letter
5th letter 6th letter

Table 1

The empty spaces are filled with '%' and then the whole grid is added from first row then second row ... to get the "encrypted word".

For example: 'america' , num_rows = 4

then, num_col = 2

a %
m a
e c
r i

Table 2

So the encrypted word is 'a%maecri'

So how to decrypt:

First we make the grid from the encrypted word. This can be easily just reverse as Table 2.

Now to get decryption, we move in the grid as in Table 1 and add letters to decrypted string(which is intially ""). We return decrypted string when we first get the '%' beacuse after that there is no need to go.

Points to note:

1. We have to run a initial loop as number of times as number of column.

2. Not in even columns, we have to move from up to down in the column(through rows) and in odd columns we have to move from down to up.

Here is the code:

import math
def sbu_decrypt(encrypted,num_rows):
    if num_rows <=0:                    #Return encrypted if num_rows<=0
        return encrypted
    if encrypted=="":                   #Return None id encrypted is empty string
        return None
    num_cols = len(encrypted)//num_rows    #Get num_cols. (Note that len(encrypted)=num_rowss*num_cols)
    k = 0
    grid = [['' for i in range(num_cols)] for j in range(num_rows)] #Make a grid
    '''Fill the grid from encrypted'''
    for i in range(num_rows):
        for j in range(num_cols):
            grid[i][j] = encrypted[k]
            k+=1
    decrypted = ""                     #Empty string to store decrypted letters
    '''In even columns move from top to bottom
    And in odd columns move from bottom to top'''
    for col in range(num_cols):
        if col%2 == 0:
            row = 0
        else:
            row = num_rows-1
        k = 0
        while(k<num_rows):       #Moving in a column(through rows)
            if grid[row][col]=="%":
                return decrypted       #Retirn whenever % is encountered
            decrypted+=grid[row][col]
            if col%2 == 0:
                row+=1
            else:
                row-=1
            k+=1
    return decrypted

print(sbu_decrypt('AMERICA',7))
print(sbu_decrypt('SOO%TRK%OBUVNYNI',4))
print(sbu_decrypt('CHICKENWINGS',-2))
print(sbu_decrypt('',5))
print(sbu_decrypt('Srs%tei%ovt%niy%ynN%BUekrkwrooYo',8))
print(sbu_decrypt('UtetasOmecanidStefAri%',2))

Screenshots:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function for Euler methods with python. Then find and write a test to prove...
Write a function for Euler methods with python. Then find and write a test to prove the code is right.
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Write a python function that will taken in a df (dataframe) and return a dictionary with...
Write a python function that will taken in a df (dataframe) and return a dictionary with the corresponding data types. You may need to instantiate an empty dictionary in your function. The keys will be the column headers of the df and the values will be the data types.
python code Write the function definition for a function which accepts one value from the main...
python code Write the function definition for a function which accepts one value from the main program and prints three times the number that is sent to it. For example, if the program sent 8, the function would print 24.
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Python Jupyter Notebook Write a python function called profit that takes the number of meals (n)...
Python Jupyter Notebook Write a python function called profit that takes the number of meals (n) sold by a restaurant in a month and calculates the monthly profit given the following information: The selling price of each meal is 12 USD (same for all meals) There is a fixed cost of 2500 USD per month regardless of the number of meals sold. There is a variable cost of 6 USD per each meal sold profit= total revenue- total cost =...
Python Question: Write a function which returns the sum of squares of the integers 1 to...
Python Question: Write a function which returns the sum of squares of the integers 1 to n. For example, the sum of the squares from 1 to 4 is 1 + 4 + 9 + 16, or 30. You may choose what should be returned if n == 0 You may not use the built-in Python sum function. The Solution Must be recursive >>> sum_of_squares(1) 1 >>> sum_of_squares(4) 30 >>> sum_of_squares(8) # 64 + 49 + 36 + ... +...
Write a Python function evaluateFunction() that takes one floating point number x as its argument and...
Write a Python function evaluateFunction() that takes one floating point number x as its argument and returns 3√(log⁡|x| )+3(x^3) The math module has a square root function and a logarithm function
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT