Question: Problem 3 Define a class called Rle with a method Rle.__init__(self, values, lengths = None satisfying the following criteria: An Rle instance contains a


Problem 3 Define a class called Rle with a method Rle.__init__(self, values, lengths = None satisfying the following criteria: An Rle instance contains a run-length encoded object An Rle instance has list attributes called values and lengths If __init__() parameter lengths is None, then encode values using RLE Otherwise, initialize the attributes from the parameters Examples: In x = Rle (["hi", "hi", "hi", "lo", "lo", "hi", "lo", "lo", "10"]) In x.values Out: ['hi', 'lo', 'hi', 'lo'] In x.lengths Out: [3, 2, 1, 3] In y = Rle (["no", "yes", "no"], [3, 3, 11) In y.values Out: ['no', 'yes', 'no'] In y.lengths Out: [3, 3, 1] Problem 4 Define the method Rle. __getitem__(self, i) satisfying the following criteria: Returns the item at offset i of the decoded sequence . Should work for both positive and negative indices You do NOT need to implement slicing Examples: In: y = Rle (["no", "yes", "no"], [3, 3, 1]) In y [0] Out: 'no' In y [3] Out: 'yes' In y[-1] Out: 'no' Problem 5 Define the method Rle.append(self, value) satisfying the following criteria: Appends a new item value to the run-length encoded object Mutates the self instance and returns None Examples: In : y = Rle (["no", "yes", "no"], [3, 3, 11) In In y.values Out: ['no', 'yes', 'no', 'yes'] y.append("yes") In y.lengths Out: [3, 3, 1, 1] In y.append("yes") In y.values Out: ['no', 'yes', 'no', 'yes'] In y.lengths Out: [3, 3, 1, 2] 2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
