Skip to content

Instantly share code, notes, and snippets.

@dvoiss
Created August 9, 2012 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvoiss/3301861 to your computer and use it in GitHub Desktop.
Save dvoiss/3301861 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer Challenge: Run Length Encoding
#!/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