Follow

Today's Freebie. It's marked as easy.

This problem was asked by Amazon.

Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".

Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.

SPOILER ONE-LINER SOLUTIONS IN PYTHON 

import re

encooded = "14A3B2C1D2A"
decoded = ""

# first attempt at decoding
list1 = re.findall('\d+\D', encooded)
for l in list1:
count, char = int(l[0:-1]) , l[-1]
decoded += ''.join(count * char)
print(decoded)

# one-liner using regex for decoding!
print(''.join([ int(l[0:-1]) * l[-1] for l in re.findall('\d+\D', encooded) ]))

# one liner using regex for encoding
print( ''.join([ str(len(match[1])+1) + match[0] for match in re.findall(r"(.)(\1*)", decoded)]))

Another Python Using Generators 

"""Example using generators for run length encode/decode."""

def rleunits(str1):
"""Generate run length encoded units."""
current = None
count = 0

for letter in str1:
if letter == current:
count += 1
else:
if current:
yield str(count)+current
count = 1
current = letter

def rldunits(str1):
"""Generate run length decoded units."""
count = None

for c in str1:
if c.isdigit():
if not count:
count = c
else:
count += c
else:
yield(c * int(count))
count = None

def main():
"""Drive the example."""
str1 = "AAAABBBBCDDEEFFFFFFFFFFF"

encoded = ''.join(rleunits(str1))
print(encoded)

unencoded = ''.join(rldunits(encoded))
print(unencoded)

main()

@Surasanji as soon as you send the check I will have a version with your name on it.. This one looks really easy though, the trick is making it pythonic. I can almost imagine a regex solution.

Basic non regex solution 

@Absinthe@qoto.org
def encode(input):
out = ""

let = input[0]
num = 1

for i in range(1, len(input)):
if input[i] == input[i-1]:
num = num + 1
else:
out = out + str(num) + input[i-1]
let = input[i]
num = 1

out = out + str(num) + input[len(input)-1]

return out

def decode(input):
out = ""

i = 0

while i != (len(input)):
for p in range(0, int(input[i])):
out = out + input[i+1]
i = i + 2

return out

Basic non regex solution 

@matrix07012 not sure how you feel about criticism, so I will hold back too much opinion. However, line 4 and 12 are unused.

Basic non regex solution 

@Absinthe@qoto.org No problem, I looked up the proper solution now and mine is clumsier than I thought.

Basic non regex solution 

@matrix07012 I just watched a great video on looping and iterators and perhaps I am a bit more critical of things:

youtube.com/watch?v=EnSu9hHGq5

It might change your life, it will change your code!

BTW, did you check out my generator version?

Basic non regex solution 

@Absinthe@qoto.org I just did, pretty interesting.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.