This doubt is with respect to the Python learning track. More specifically the ‘Practice Makes Perfect - Reverse’ exercise.
I wrote a code to execute the required switches in index, but my code isn’t functioning well. It seems to give the correct output if I enter odd character strings but not with even character strings. The code relies on evaluating the mean of the indices, the difference of index from the mean, and then adding/subtracting (based on whether the original index is above or below the mean) that difference to the mean to get the required reversed index. I would be grateful if someone can point out the flaw here. Thanks
def reverse(text): l = range(len(text)) mean = sum(l)/len(text) for i in l: if i<mean: x=mean+abs(mean - i) print text[x], else: x= mean - abs(mean -i) print text[x],