Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Bonus: Recursive Encryption (3 points) Privacy has been a major talking point amongst many tech giants as of the past 2 years. Let's be creative
Bonus: Recursive Encryption (3 points) Privacy has been a major talking point amongst many tech giants as of the past 2 years. Let's be creative and find a way to encrypt our messages to send to one another! Define a function encrypt which takes 3 arguments msg - a non-empty string message shift - a non-negative integer func - a lambda function that takes one integer argument and returns an integer. For the math lovers (which means all of you) the type definition looks like this: Int Int The secret sauce to this encryption method is that it will shift every character by some integer amount shift. For example 'h' shifted 5 characters is 'm'. When shift is positive value, the shifted char will move in the (right) direction based on nominal value. (Consult Ascii chart for more info: http://www.asciitable.com/) Therefore, we will go through every character c in the string and modify each by increasing the ordinalominal value by shift creating a new character c' (pronounced C-prime or /s/yah/prm/). Here's the catch, the shift for the next character will always change, as dictated by the lambda function func. The lambda function should receive as argument the ordinal value of c'( ord(c')) from that recursive call and return you a new shift integer value... (Oh yea, do this recursively; you'll thank me later.) But in the iterative sense, func supplies you with the next shift value for the next proceeding character. HINT: With a recursive solution, this can be as little as 2 lines of concise code, first defining a base case (string of length 1), and then computing the solution in terms of its own smaller subproblems (the remainder of string). WINK: Use ord and chr built ins. #Your code here def encrypt(msg, shift, func): return print("Are my sample test cases correct?") print("#1", encrypt("hello",1, lambda x: (x + 1) % 10 ) == "iktsu") print("#2", encrypt("covfefe", 13, lambda x: x % 13 if x % 2 == 0 else x x 3) == "pwxiehe") print("#3", encrypt("precious", 12, lambda x: x + 1) == "Trgo")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started