Run Length Encoding in a String in Python

Accessing each character in a string is possible using index slicing. Index 0 can be used to access the first character and so on.  For eg: str=”Happy”, the character ‘p’ can be accessed at both index 2,3. This tutorial is focussed on the topic: Run Length Encoding in a String in Python.

 

To know the methods or functions related to String Datatype, one can type in

>>help(str)

in the Python IDLE.

Program: Run Length Encoding in a String in Python


from collections import OrderedDict
def Length_Encoding():
      st=input("Enter a string")
      Diction=OrderedDict.fromkeys(st,0)
      for i in st:
             Diction[i]+=1
      new_str=''
      for k,v in Diction.items():
             new_str=new_str+k+st(v)
      print(new_str)

 

OUTPUT


Enter a string happuy 
h1a1p2u1y1

CODE EXPLANATION: Run Length Encoding in a String

In the Code Above, we use a user-defined function “Length_Encoding” to emphasize the desired output.  To Run ” LENGTH ENCODING” of a string means to specify the number of times a character is used in a string written after that particular character.

For eg: if the string is “happy birthday” then the desired output should be  “h2a2p2y2b1i1r1t1d1

We imported the “OrderedDict” from the “collections” library. The first question coming to your mind would be “What is OrderdDict and why to use it?”

OrderedDict in Python is a subclass of the main class of dictionary datatype. It maintains the items in their Insertion order, unlike normal dictionary dict that does not track the insertion order. It is a part of the collections module in python.

The traversal and returning(printing) of key-value pairs in OrderedDict is done in the order in which values are inserted.

Then in the user-defined function,

Input the desired string

  • Using the variable st, one can enter a string of their choice on which they want to perform this function using the Python input() function.

Convert the string to dictionary

  • Then fromkeys() method is used from the “OrderedDict” library.
  • This method will create a dictionary with each character of the string as a key and initiating their values to ‘0’.
  • The values would indicate the number of occurrences of the characters in the string is present.
  • This dictionary is allocated to the variable Diction.
  • for “happuy” the status now should be {‘h’:0,’a’:0,’p’:0,’u’:0,’y’:0}

Assign the value to each string as the number of occurrences of each character

  • Now using a for loop we get the number of times a character occurs in the string as the value of the respective character in the dictionary Diction.
  • for “happuy” the status would now be {‘h’:1,’a’:1,’p’:2,’u’:1,’y’:1}.

Convert the above dictionary to a string

  • Create a new blank string by the variable name new_str
  • Using a for loop, we are going the traverse the keys and values of the dictionary.
  • For every traversal, the key and its value( typecasted to string)  are concatenated to the string and as soon as the traversal is done, it would result in a Length encoded string.
  • Then the new string by the name new_str is printed.
  • So now, the dictionary {‘h’:1,’a’:1,’p’:2,’u’:1,’y’:1}  changes to “h1a1p2u1y1

Leave a Reply

Your email address will not be published. Required fields are marked *