def last_element(lst):
if not lst:
return None
elif type(lst[-1]) == tuple:
return lst[-1][1]
else:
return lst[-1]
def increment_last(lst,x):
if not lst:
lst.append(x)
elif type(lst[-1]) == tuple:
count = lst[-1][0]+1
else:
count = 2
lst[-1] = (count, x)
def encode_frugal(lst):
result = []
for x in lst:
if x == last_element(result):
increment_last(result,x)
else:
result.append(x)
return result
Here's another one that seems a little more verbose than necessary, but that's what I come up with for a first pass.
No comments:
Post a Comment