Using Python 2.7, imagine I have two variables:
encoded_username = 'bender_in_soup_1234'
code = '000204071012'
The string in code actually specifies the position of upper case characters in encoded_username. I.e. 00 implies the first character is upper case. 02 implies the third character is upper case. 04 implies the fifth character is upper case. And so on. It can possibly go to 99 (usernames are not longer than 100 characters).
The fully decoded username is:
decoded_username = 'BeNdEr_In_SoUp_1234'
What's the most efficient way (in Python 2.7) to decode the encoded_username using the code provided?
I'm trying:
upper_case_positions = [code[i:i+2] for i in range(0,len(code),2)]
for position in upper_case_positions:
encoded_username[position] = encoded_username[int(position)].upper()
return encoded_uname
But this simply gives me 'str' object does not support item assignment.
Moreover, I'm to parse through multiple usernames while decoding them, i.e. the code above is going to be nested inside a for loop. Having for loops within for loops makes me feel there could have been a more efficient solution. What do you folks suggest? I'm highly intrigued.