python - Program for word reversal randomly skips out letters? -


my program keeps randomly skipping out letters! example, 'coolstory' becomes 'yrotsloc' , 'awesome' becomes 'mosewa'

here code:

def reverse(text):     length = len(text)     reversed_text = []     in range(0,length + 1):         reversed_text += ['']     original_list = []     l in text:         original_list.append(l)         new_place = length - (original_list.index(l))         reversed_text[new_place] = l     return "".join(reversed_text) 

this happens when have duplicate letters because

original_list.index(l) 

will return same value same l. new_place same 2 of same letters @ different locations.

one common way reverse strings in python slicing:

>>> s = "hello" >>> s[::-1] 'olleh' 

you can use reversed(), returns reversed object (not string). better option if want iterate on string in reverse order:

>>> c in reversed(s): ...     print c ...  o l l e h 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -