Ask Question
21 January, 15:29

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 If it is, use the current char to get the value from custom_encodings, and concatenate it to custom_encoded The line inside the if should look something like out = out + dictionary[char] Otherwise, concatenate the current character to custom_encoded

+5
Answers (1)
  1. 21 January, 19:07
    0
    custom_encoded = "" custom_message = "this is a pen" custom_encoding = { "a": "1", "e": "2", "i": "3", "o": "4", "u": "5" } for c in custom_message: if (c in custom_encoding) : custom_encoded = custom_encoded + custom_encoding[c] else: custom_encoded = custom_encoded + c print (custom_encoded)

    Explanation:

    The solution code is written in Python 3.

    Let's define a variable custom_encoded with an empty string (Line 1).

    Create another variable custom_message and set a sample string to it (Line 2).

    Create one more variable custom_encoding to hold the mapping values between vowel and their respective encoded character (Line 3-9).

    Create a for loop to loop through all the character in custom_message (Line 11) and in the loop check if the current character is found in custom_encoding dictionary, if so, use the current character to loop up the dictionary for the encoded value and add the encoded string to custom_encoded (Line 12-13). Otherwise, just add the current character to custom_encoded (Line 14-15).

    At the end, print the custom_encoded to terminal and you shall get th3s 3s 1 p2n
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a code block to encode strings from original messages, using our custom encoding. To do so: Initialize a variable called ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers