Question
16. Write a code block to encode strings from original messages, using our custom encoding. custom_encodings = {'a' : 'r' , 'e' : 'p' ,
16. Write a code block to encode strings from original messages, using our custom encoding. custom_encodings = {'a' : 'r' , 'e' : 'p' , 'i' : 'm' , 'o' : 'n' , 'u' : 's'}
To do so: - Initialize a variable called custom_encoded as an empty string - Use a for loop to loop across all charcaters of a presumed string variable called custom_message . - inside the loop, check if the current character is in the. custom_encodings dictionary - if it is, use the current char to get the value from custom_encodings and concatenate it to custom_encoded - the line inside should look like out= out+ dictionary[char] - otherwise concatenate the current character to custom_encoded.
17. This code will presume two numbers, start_key and key_increment. use key = start_key before the loop. the loop will contain the same code to encode each character but you need to add a loop wothin the loop , after that encoding, that updates the key variable to increment it by the value stored in key_increment.
Write a code block to encode strings from original messages, using our custom encoding. To do so: Initialize a variable called custom_encoded as an empty string Use a for loop to loop across all characters of a presumed string variable called custom_message Inside the loop, check if the current character is in the custom_encodings dictionary o If it is, use the current char to get the value from custom_encodings, and concatenate it to custom_encoded o The line inside the if should look something like out = out + dictionary[char] o Otherwise, concatenate the current character to custom_encoded In [1]: %%writefile A2 Code/custom_encoder.py # YOUR CODE HERE Overwriting A2Code/custom_encoder.py In [95]: custom_message = 'vowels aint great' %run -i ./A2Code/custom_encoder.py assert isinstance(custom_encoded, str) assert custom_encoded == 'vnwpls rmnt grprt' Validate Show Usage Q17 - Encodings with variable keys (0.5 points) Here we will write an encoder that uses variable keys, whereby each time a key is applied, the key itself is also updated by adding a number. This code will presume two numbers, start_key and key_increment. This code will look very similar to the code for the encoder in Q15. Copy that code in, and you can use key = start_key before the loop. The loop will contain the same code to encode each character, but you need to add a line within the loop, after the encoding, that updates the key variable to increment it by the value stored in key_increment In [101]: %%writefile A2Code/variable_encoder.py # YOUR CODE HERE def encoder (message, key): encoded = key = start_key for variable in message: unicode = ord(variable) temp_value = unicode + key encoded_char = chr(temp_value) encoded += encoded_chr key += key_increment Overwriting A2 Code/variable_encoder.py 12:08 PM
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