def realLengthEncode(string): current = string[0] count = 1 toReturn = '' for char in string[1:]: if char == current: count += 1 else: toReturn += str(count)+current current = char count = 1 toReturn += str(count) + current return toReturn def realLengthDecode(string): index = 0 toReturn = '' while index < len(string): count = int(string[index]) letter = string[index+1] for i in range(count): toReturn += letter index += 2 return toReturn decoded = 'aaaabcccaa' encoded = realLengthEncode(decoded) print encoded decoded2 = realLengthDecode(encoded) print decoded == decoded2
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question