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.
Write a Python function that computes the frequency of characters in a string s. The function...
Write a Python function that computes the frequency of characters in a string s. The function should return a map where the key is the character and the value is the count. Make sure you include a main function and include comments to document your code.
PYTHON: Write a function that takes in a number as a parameter and calculates the square...
PYTHON: Write a function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter. The function must perform the process recursively.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python: Write a recursive function that takes in a number as a parameter and calculates the...
Python: Write a recursive function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter.
Python: Write a function that takes in a number as a parameter and calculates the square...
Python: Write a function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter. Please use a while or for loop.
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT