Project #15070 - Python - using strings and defining functions

In this assignment, you will practice using strings and defining functions. You need to implement a program that encrypts and decrypts messages, using a variant of the Rail Fence Cipher. 

Variant Rail Fence Cipher

In the rail fence cipher, the message to be encrypted is written downwards and diagonally on successive "rails" of an imaginary fence, moving to the top when you reach the bottom, and continuing the same way till the whole text is written out. The encrypted message is then read in rows. In order to decrypt the message, the process is reversed.

For this assignment you can assume that the fence has either 3 or 2 rails.

W . . R . . I . . O . . R . . G
. E . . E . . S . . V . . E . . O
. . A . . D . . C . . E . . D . . 

3 rail fence of the string "WEAREDISCOVEREDGO"

The result is "WRIORGEESVEOADCED"

 

W . A . E . I .  C . V . R . D. O
. . E . R . D .S . O. E . E. G . 

 

2 rail fence of the string "WEAREDISCOVEREDGO" 

the result is "WAEICVRDOERDSOEEG"

For this assignment you will have to create two functions: encrypt() and decrypt(). Both functions should have two parameters: (1) the text and (2) the number of rails; they both return the resulting encrypted or decrypted string. You will also have to write a main program that will get input from the user as specified below.

 

The user of the program specifies on a single line of input: (1) the mode ("E" for encryption or "D" for decryption); (2) the text in double quotes; and (3) the number of rails that should be used (2 or 3). The third is optional, if there is no number there, the default number of rails is 2 (see the example). The program terminates when the user enters an empty string, without any message. For example:

E "WEAREDISCOVEREDGO" 2

WAEICVRDOERDSOEEG

E "WEAREDISCOVEREDGO"

WAEICVRDOERDSOEEG

D "WAEICVRDOERDSOEEG" 2

WEAREDISCOVEREDGO

d "WAEICVRDOERDSOEEG" 2

WEAREDISCOVEREDGO

B "WAEICVRDOERDSOEEG" 2

invalid encryption command

D "WAEICVRDOERDSOEEG" 4

invalid number of rails

E      "WE ARE DISCOVERED GO" 3

WA SVEGERDCEDO EIOR 

 

 

For your program the number of spaces outside the "" of the input should not change anything on the result. Spaces inside the string to be encrypted/decrypted should be taken into account for the result.

Subject Computer
Due By (Pacific Time) 10/23/2013 12:00 am
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy