
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to implement run length string decoding iterator class in Python
Suppose we want to define an iterator class that constructs with a run-length encoded lowercase string say s, there are two functions for this iterator they are −
- next() this finds the next element in the iterator
- hasnext() this checks whether the next element is present or not
So, if the input is like s = "2b1a", then construct an object with s, then call next(), hasnext(), next(), next(), hasnext(), then the output will be "b", True, "b", "a", False.
To solve this, we will follow these steps −
- Define a constructor. This will take s
- output := a new list
- num := blank string
- for each i in s, do
- if i is alphabetic, then
- insert num at the end of output
- insert i at the end of output
- num := blank string
- otherwise,
- num := num + i
- if i is alphabetic, then
- Define a function next().
- if hasnext() is true, then
- count := output[0]
- letter := output[1]
- count := count - 1
- if count > 0, then
- output[0] := output[0] - 1
- otherwise,
- output := output[from index 2 to end]
- return letter
- Define a function hasnext().
- if size of output is not 0, then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
class RunLengthIterator: def __init__(self, s): self.output = [] num = "" for i in s: if i.isalpha(): self.output.append(int(num)) self.output.append(i) num = "" else: num += i def next(self): if self.hasnext(): count = self.output[0] letter = self.output[1] count -= 1 if count > 0: self.output[0] -= 1 else: self.output = self.output[2:] return letter def hasnext(self): if len(self.output) != 0: return True return False s = "2b1a" obj = RunLengthIterator(s) print(obj.next()) print(obj.hasnext()) print(obj.next()) print(obj.next()) print(obj.hasnext())
Input
"2b1a" obj = RunLengthIterator(s) obj.next() obj.hasnext() obj.next() obj.next() obj.hasnext()
Output
b True b a False
- Related Articles
- C++ program to implement Run Length Encoding using Linked Lists
- Program to encode a string in normal form to its run-length form in Python
- Program to decode a run-length form of string into normal form in Python
- Program to find minimum length of lossy Run-Length Encoding in Python
- Run Length Encoding in Python
- Program to find dot product of run length encoded vectors in Python
- Python Program to implement switch statement on String
- Program to find min length of run-length encoding after removing at most k characters in Python
- Python program to remove K length words in String
- How to run Python Program?
- Iterator class in Dart Programming
- Python program to print even length words in a string
- C++ Program to Implement Variable Length Array
- How to Run a Python Program
- Program to Implement Queue in Python

Advertisements