the Encoding scheme is given below:
Don't replace the characters in the even places. Replace the characters in the odd places by their place numbers . and if they exceed 'z', then again they will start from 'a'.
example: for a message "hello" would be "ieolt" and for message "maya" would be "naba"
how many lines of code [minimum] do we have to write in order to encode a lengthy string in python in the above format.?
mycode is given below:
def encode(msg):
encoded_message = ""
letters = "abcdefghijklmnopqrstuvwxyz"
index_of_msg = 0
for char in msg.lower():
if char.isalpha():
if index_of_msg%2 == 0 :
encoded_message += letters[((letters.rfind(char) )+ (index_of_msg+1))%26] # changed msg.rfind(char) to index_of_msg
else:
encoded_message += char
else:
encoded_message += char
index_of_msg +=1
return encoded_message
hello
, shouldn't the first letter be replaced bya
(place == 1)? Edit: oh, I get it, you add the place number to the letter. – Bogdan Aug 14 '13 at 13:04hello
as h is in odd place ie 1 thus, it moved by its place number 1 ie h+1 = i Sorry if my actual question confused you – Sandeep Aug 14 '13 at 13:08