Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rle(word):
- out = []
- back = 0
- for front, char in enumerate(word):
- if not char.isalpha() or not char.isupper():
- raise Exception('char is not upper case letter:' + char)
- if front == len(word) - 1 or char != word[front+1]:
- out.append(word[back])
- count = front + 1 - back
- if count > 1:
- out.append(str(count))
- back = front+1
- return ''.join(out)
- assert rle('AABB') == 'A2B2'
- assert rle('AAAABBBCCXYZ') == 'A4B3C2XYZ'
- assert rle('') == ''
- # rle('AaA')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement