The first function is able to separate each letter of a string and list how many times that letter appears. For example:
print(rlencode("Hello!"))
[('H', 1), ('e', 1), ('l', 2), ('o', 1), ('!', 1)]
How do I get rldecode(rle)
: do the the complete opposite of rlencode(s)
so that rldecode(rlencode(x)) == x
returns True
def rlencode(s):
"""
signature: str -> list(tuple(str, int))
"""
string=[]
count=1
for i in range(1,len(s)):
if s[i] == s[i-1]:
count += 1
else:
string.append((s[i-1], count))
count=1
if i == len(s)-1:
string.append((s[i], count))
return string
def rldecode(rle):
"""
#signature: list(tuple(str, int)) -> str
#"""
string=" "
count=1
for i in rle:
if i == rle:
string += i
return string