Open In App

Run Length Encoding in Python

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an input string, write a function that returns the Run Length Encoded string for the input string. For example, if the input string is ‘wwwwaaadexxxxxx’, then the function should return ‘w4a3d1e1x6’. 

Examples:

Input  :  str = 'wwwwaaadexxxxxx'
Output : 'w4a3d1e1x6'

This problem has existing solution please refer Run Length Encoding link. Here we will solve this problem quickly in python using OrderedDict. Approach is very simple, first we create a ordered dictionary which contains characters of input string as key and 0 as their default value, now we run a loop to count frequency of each character and will map it to it’s corresponding key. 

Implementation:

Python3




<div id="highlighter_850800" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments"># Python code for run length encoding</code></div><div class="line number2 index1 alt1"><code class="keyword">from</code> <code class="plain">collections </code><code class="keyword">import</code> <code class="plain">OrderedDict</code></div><div class="line number3 index2 alt2"><code class="keyword">def</code> <code class="plain">runLengthEncoding(</code><code class="functions">input</code><code class="plain">):</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="undefined spaces">    </code><code class="comments"># Generate ordered dictionary of all lower</code></div><div class="line number6 index5 alt1"><code class="undefined spaces">    </code><code class="comments"># case alphabets, its output will be</code></div><div class="line number7 index6 alt2"><code class="undefined spaces">    </code><code class="comments"># dict = {'w':0, 'a':0, 'd':0, 'e':0, 'x':0}</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="functions">dict</code><code class="keyword">=</code><code class="plain">OrderedDict.fromkeys(</code><code class="functions">input</code><code class="plain">, </code><code class="value">0</code><code class="plain">)</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="comments"># Now iterate through input string to calculate</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="comments"># frequency of each character, its output will be</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="comments"># dict = {'w':4,'a':3,'d':1,'e':1,'x':6}</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="keyword">for</code> <code class="plain">ch </code><code class="keyword">in</code> <code class="functions">input</code><code class="plain">:</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="functions">dict</code><code class="plain">[ch] </code><code class="keyword">+</code><code class="keyword">=</code> <code class="value">1</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="undefined spaces">    </code><code class="comments"># now iterate through dictionary to make</code></div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code><code class="comments"># output string from (key,value) pairs</code></div><div class="line number18 index17 alt1"><code class="undefined spaces">    </code><code class="plain">output </code><code class="keyword">=</code> <code class="plain">''</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">    </code><code class="keyword">for</code> <code class="plain">key,value </code><code class="keyword">in</code> <code class="functions">dict</code><code class="plain">.items():</code></div><div class="line number20 index19 alt1"><code class="undefined spaces">        </code><code class="plain">output </code><code class="keyword">=</code> <code class="plain">output </code><code class="keyword">+</code> <code class="plain">key </code><code class="keyword">+</code> <code class="functions">str</code><code class="plain">(value)</code></div><div class="line number21 index20 alt2"><code class="undefined spaces">    </code><code class="keyword">return</code> <code class="plain">output</code></div><div class="line number22 index21 alt1"> </div><div class="line number23 index22 alt2"><code class="comments"># Driver function</code></div><div class="line number24 index23 alt1"><code class="keyword">if</code> <code class="plain">__name__ </code><code class="keyword">=</code><code class="keyword">=</code> <code class="string">"__main__"</code><code class="plain">:</code></div><div class="line number25 index24 alt2"><code class="undefined spaces">    </code><code class="functions">input</code><code class="keyword">=</code><code class="string">"wwwwaaadexxxxxx"</code></div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="functions">print</code> <code class="plain">(runLengthEncoding(</code><code class="functions">input</code><code class="plain">))</code></div></div></td></tr></tbody></table></div>


Output

w4a3d1e1x6

Another code: 

Python3




def encode(message):
    encoded_message = ""
    i = 0
 
    while (i <= len(message)-1):
        count = 1
        ch = message[i]
        j = i
        while (j < len(message)-1):
            if (message[j] == message[j+1]):
                count = count+1
                j = j+1
            else:
                break
        encoded_message=encoded_message+str(count)+ch
        i = j+1
    return encoded_message
 
#Provide different values for message and test your program
encoded_message=encode("ABBBBCCCCCCCCAB")
print(encoded_message)


Output

1A4B8C1A1B

 



Similar Reads

Minimum length of Run Length Encoding possible by removing at most K characters from a given string
Given a string S of length N, consisting of lowercase English alphabets only, the task is to find the minimum possible length of run-length-encoding that can be generated by removing at most K characters from the string S. Examples: Input: S = "abbbcdcdd", N = 9, K = 2 Output: 5 Explanation: One possible way is to delete both occurrences of 'c' fro
10 min read
Run Length Encoding and Decoding
Given an input string, write a function that returns the Run Length Encoded string for the input string.For example, if the input string is "wwwwaaadexxxxxx", then the function should return "w4a3d1e1x6" Recommended PracticeRun Length EncodingTry It! Follow the steps below to solve this problem: Pick the first character from the source string. Appe
4 min read
Program to implement Run Length Encoding using Linked Lists
Given a Linked List as the input. The task is to encode the given linked list using Run Length Encoding. That is, to replace a block of contiguous characters by the character followed by it's count. For Example, in Run Length Encoding "a-&gt;a-&gt;a-&gt;a-&gt;a" will be replaced by "a-&gt;5". Note: For non-repeating nodes, do not append count 1. Fo
15+ min read
Python | C Strings of Doubtful Encoding | Set-2
String handling in extension modules is a problem. C strings in extensions might not follow the strict Unicode encoding/decoding rules that Python normally expects. Thus, it’s possible that some malformed C data would pass to Python. A good example might be C strings associated with low-level system calls such as filenames. For instance, what happe
3 min read
Python | C Strings of Doubtful Encoding | Set-1
One can convert strings between C and Python vice-versa but the C encoding is of a doubtful or unknown nature. Let's suppose that a given C data is supposed to be UTF-8, but it’s not being strictly enforced. So, it is important to handle such kind of malformed data so that it doesn’t crash Python or destroy the string data in the process. Code#1 :
2 min read
Python | Character Encoding
Finding the text which is having nonstandard character encoding is a very common step to perform in text processing. All the text would have been from utf-8 or ASCII encoding ideally but this might not be the case always. So, in such cases when the encoding is not known, such non-encoded text has to be detected and the be converted to a standard en
2 min read
Python | Encoding Decoding using Matrix
This article is about how we use the matrix to encode and decode a text message and simple strings. Encoding process : Take a String convert to corresponding number shown below convert to 2D matrix(array). Now we have 2x2 matrix! When we multiply this matrix with encoding matrix we get encoded 2x2 matrix. now convert to vector(1D array) and Display
4 min read
Python - Golomb Encoding for b=2n and b!=2n
The Golomb coding is a form of parameterized coding in which integers to be coded are stored as values relative to a constant b Coding:- A positive number x is spoken to in two sections: The initial segment is an unary portrayal of q+1, where q is the remainder floor((x/b)), andThe subsequent part is an extraordinary double portrayal of the leftove
3 min read
response.encoding - Python requests
Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object would be used to access certain features such as content, headers, etc. This article revolves around how to check the response.encoding out of a
2 min read
Elias Gamma Encoding in Python
The Elias gamma code is a universal code that is used to encode a sequence of positive integers. It is developed by Peter Elias. It is most useful when the upper bound of integers cannot be determined beforehand. Steps in Encoding: To encode a number X, Find the largest N, with [Tex]2^{N}\leq X[/Tex](greater power of 2). Encode N using Unary coding
1 min read
Practice Tags :
three90RightbarBannerImg