Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:02:57+00:00 2026-06-07T03:02:57+00:00

The RLE (run length encoding) pattern seems to come up a lot in my

  • 0

The RLE (run length encoding) pattern seems to come up a lot in my work.

The essence of it is that you are outputting a reduction of the elements encountered since the last ‘break’ each time that you see a ‘break’ or you reach the end of the input.

(In actual RLE, the ‘break’ is just this character not matching the last character, but in the real world it’s usually a little more complex, but still a function of the current and last elements.)

I want to remove the duplicate last_val != None: rle.append((last_val, count)) condition and action which occur both in the loop and at the end.

The issues are:

  1. replacing them with function calls results in more code, not less.
  2. keeping it in imperative style (in Haskell, for example, the problem just evapourates).

The imperative Python code is:

#!/usr/bin/env python

data = "abbbccac"

if __name__ == '__main__':
  rle = []
  last_val = None
  count = 0;

  for val in data:
    if val != last_val and last_val != None:
      rle.append((last_val, count))
      count = 1
    else:
      count += 1
    last_val = val
  if last_val != None:
    rle.append((last_val, count))

  print rle

P.S. Trivially solvable in functional languages:

#!/usr/bin/env runhaskell
import Data.List (group)

dat = "abbbccac"

rle :: Eq a => [a] -> [(a, Int)]
rle arr = map (\g -> (head g, length g)) $ group arr

main :: IO ()
main = print $ rle dat
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:02:59+00:00Added an answer on June 7, 2026 at 3:02 am

    Here is a more imperative form. You can eliminate your duplicate code by adding or chaining to a throwaway sentinel that will never match any of your list elements, forcing an end-of-sequence pass through your “this-not-equal-last” code:

    from itertools import chain
    
    def rle(seq):
        ret = []
        sentinel = object()
        enum = enumerate(chain(seq,[sentinel]))
        start,last = next(enum)
        for i,c in enum:
            if c != last:
                ret.append((last,i-start))
                start,last = i,c
        return ret
    

    This even gracefully handles the case where the input seq is empty, and the input can be any sequence, iterator, or generator, not just a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a RLE (Run-Length Encoder) Programme just for characters. I read
Which one of these would be more suitable for RLE on bytes? I need
The classical RLE algorithm compresses data by using numbers to represent how many times
...if that is possible My task is to find the longest streak of continuous
This problem seems trivial but I'm at my wits end after hours of reading.
I have difficulties with how to use RLE on sequences of symbols. For example,
I am getting some very surprising results that seem to indicate that it's more
I have a bunch of image files in a proprietary binary format that I
I am not very good at c at all. I have tried to capture
Is there an efficient, quick and simple example of doing differential b/w image compression?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.