알고리즘 문제/CodeWar
Decipher this!
BEstyle
2022. 10. 25. 13:17
DESCRIPTION:
You are given a secret message you need to decipher. Here are the things you need to know to decipher it:
For each word:
- the second and the last letter is switched (e.g. Hello becomes Holle)
- the first letter is replaced by its character code (e.g. H becomes 72)
Note: there are no special characters used, only letters and spaces
Examples
decipherThis('72olle 103doo 100ya'); // 'Hello good day'
decipherThis('82yade 115te 103o'); // 'Ready set go'
def decipher_this(string):
ans=[]
alist=string.split(" ")
for j in range(len(alist)):
temp=""
tempWord=""
for i in range(len(alist[j])):
if alist[j][i] in "0123456789":
temp+=str(alist[j][i])
else:
tempWord+=alist[j][i]
tempWord=chr(int(temp))+tempWord
if (len(tempWord))>2:
temp=tempWord[0]+tempWord[len(tempWord)-1]
for i in range(2, len(tempWord)-1):
temp+=tempWord[i]
tempWord=temp+tempWord[1]
ans.append(tempWord)
return " ".join(ans)