Write a function in python def encode(msg,secret_d): Consider a string msg and a
ID: 3684461 • Letter: W
Question
Write a function in python def encode(msg,secret_d): Consider a string msg and a dictionary, secret_d, where the keys are single-character strings and values are strings. For every character ch in msg, the function should search in the dictionary and replace it with the mapping string if ch is a key in secrect_d. If ch is not a key in secrect_d, it should be kept unchanged.
o msg: a string to be encoded
o secret_d: a dictionary that maps single characters to strings.
o Return value: an updated version of msg after replacement.
Examples:
encode("CAN YOU READ THIS",{'A':'4','E':'3','T':'7','I':'1','S':'5'}) 'C4N YOU R34D 7H15'
encode("S+EWWE1+++",{'+':'0','S':'7','E':'3','W':'9'}) '7039931000'
Explanation / Answer
Solution for Question :
This below python script will read the message and charcter will store in the dictionary
and will perform the encryption.
def endcode(msg,secret_d):
newstr=""
for ch in msg:
for key,value in secret_d.iteritems():
if ch == key:
newstr=newstr+value
if(ch not in secret_d):
newstr=newstr+ch
print(newstr)
endcode('CAN YOU READ THIS',{'A':'4','E':'3','T':'7','I':'1','S':'5'})
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.