Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 3a For this part you will be writing a series of functions that can be used

ID: 3759521 • Letter: P

Question

Part 3a

For this part you will be writing a series of functions that can be used as part of a "secret message encoder" program. Here are the functions you will be writing as well as some sample code that you use use to test your work.

Sample Program

Sample Output

Hint: you will need to use a loop to generate the required # of random characters, and you will (obviously) need to use some random number functions as well. Think about the algorithm before you start coding! Draw out the steps you think you need to take on a piece of paper. For example: "Start with the first character in the source word. Then generate 'num' new random characters and concatente these characters. Then move onto the next character in the source word and repeat the process".

Once you have written the add_letters function you should begin to work on the next function (remove_letters) which will perform the reverse operation.

Sample Program

Sample Output

Hint: String slicing may make your life a lot easier when writing this function!

Finally, write a function called "shift_characters" that shifts the characters in a word up or down based on their position [assignments/assignment07/ascii.png ASCII table] - here's the IPO notation and a sample program:

Sample Program

Sample Output

Hint: use the ord() and chr() functions!

Explanation / Answer

import string
import random
def add_letters(original,num):
   n=len(original)
   scrambled=""
   for i in range(n):
       scrambled+=original[i]
       randomSequence=""
       for j in range(num):
           randomSequence+=random.choice(string.letters)
       scrambled+=randomSequence
   return scrambled

original = "Hello!"
for num in range(1,5):
   scrambled=add_letters(original,num)
   print "Adding "+str(num)+" random characters to "+ original+" -> "+scrambled