Created
August 9, 2012 07:09
-
-
Save dvoiss/3301861 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer Challenge: Run Length Encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Challenge: http://www.reddit.com/r/dailyprogrammer/comments/xxbbo/882012_challenge_86_easy_runlength_encoding/ | |
# Solution: http://www.reddit.com/r/dailyprogrammer/comments/xxbbo/882012_challenge_86_easy_runlength_encoding/c5qf7af | |
# Run code online: http://ideone.com/DwxRr | |
import sys | |
import re | |
if len(sys.argv) == 1: | |
print 'Enter a string as input' | |
exit() | |
input = sys.argv[1] | |
items = [] | |
idx = 0 | |
while idx < len(input): | |
char = input[idx] | |
pattern = re.compile(r"[^" + char + "]") | |
match = pattern.search(input, idx) | |
if match: | |
items.append( (match.start() - idx, char) ) | |
idx = match.start() | |
else: | |
items.append( (len(input) - idx, char) ) | |
break | |
print items | |
bonus = '' | |
for item in items: | |
bonus += item[0] * item[1] | |
print bonus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment