Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in Python Write the unit test script to perform your testing for both functions encrypt and decrypt using the unittest module and prove that

Code in Python

Write the unit test script to perform your testing for both functions encrypt and decrypt using the unittest module and prove that they work properly. Remember to be as descriptive as possible and write as many cases as necessary

def encrypt(message, key):

"""

Takes a string and an integer an returns the encrypted message using the Caesar cipher method

>>> encrypt("Hello world",12)

'Tqxxa iadxp'

>>> encrypt("We are Penn State!!!",6)

'Ck gxk Vktt Yzgzk!!!'

>>> encrypt("We are Penn State!!!",5)

'Bj fwj Ujss Xyfyj!!!'

>>> encrypt(5.6,3)

'Invalid input'

>>> encrypt('Hello',3.5)

'Invalid input'

>>> encrypt(5.6,3.15)

'Invalid input'

"""

# --- YOU CODE STARTS HERE

if type(message) is not str or type(key) is not int:

return "Invalid Input"

encrypt_str = ""

for c in message:

if (65 ord(c) < 91)

if (ord(c) + key > 91):

c = chr(65 + ((ord(c) + key - 91) % 26))

else:

c = chr(ord(c) + key)

elif (97 ord(c) 122)

if (ord(c) + key > 122):

c = chr(97 + ((ord(c) + key -122) % 26) - 1)

else:

c= chr(ord(c) + key)

encrypt_str += c

return encrypt_str

# --- CODE ENDS HERE

def decrypt(message, key):

"""

Takes a string and an integer an returns the decrypted message using the Caesar cipher method

>>> decrypt("Tqxxa iadxp",12)

'Hello world'

>>> decrypt("Ck gxk Vktt Yzgzk!!!",6)

'We are Penn State!!!'

>>> decrypt("Bj fwj Ujss Xyfyj!!!",5)

'We are Penn State!!!'

>>> decrypt(5.6,3)

'Invalid input'

>>> decrypt('Hello',3.5)

'Invalid input'

>>> decrypt(5.6,3.15)

'Invalid input'

"""

# --- YOU CODE STARTS HERE

if type(message) is not str or type(key) is not int:

return "Invalid Input"

decrypt_str = ""

for c in message:

if (65 ord(c) 91)

if (ord(c) + key < 65):

c = chr(91 - ((ord('A') - (ord(c) - key)) % 26))

else:

c = chr(ord(c) + key)

elif (97 ord(c) 122)

if ord(c) - key < ord('a'):

c = chr(ord('z') - ((ord('a') - (ord(c) - key) % 26) + 1)

else:

c= chr(ord(c) - key)

decrypt_str += c

return decrypt_str

# --- CODE ENDS HERE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

What is job enlargement ?

Answered: 1 week ago