Binary Run-Length Encoding

3

Overview

Run-length encoding — sequences in which the same data value occurs in many consecutive data elements are stored as a single data value and count

When using run-length encoding:

Input:  ABBBCCACB
Output: 1A3B2C1A1C1B

This sample has an output that is larger than the input.


Instead, it can be simplified for binary inputs by removing the data value in the result,

Input:  10001110101
Output: 1331111

which is significantly smaller. This works because, logically, if the there is a "next subset" then it must be of a different data value than the current subset. Since there are only two possible values (1 and 0), you always know the "next" data value subset.


Challenge

Implement binary run-length encoding demonstrated above.

  • Take input through STDIN or suitable alternative.

  • Output to STDOUT or suitable alternative.


Examples

In:  101011101011000011
Out: 11113111242

In:  01001010001
Out: 11211131

In:  000000000
Out: 9

You may assume that no subsets will be longer than 9 characters (there won't be 10 adjacent zeroes).

Zach Gates

Posted 2015-12-23T21:48:49.533

Reputation: 6 152

Question was closed 2015-12-24T23:52:22.703

There is an error in your first example's output! The 1 digit after the first B shouldn't be there. – danmcardle – 2015-12-23T21:53:59.717

3@AlexA.: I don't see where conversion to binary will actually happen, and besides, I think that the shortest way to run-length encode a binary string will be appreciably different from regular run-length encoding. There might be some tricks or the like available w.r.t. binary that isn't there for other inputs. – El'endia Starman – 2015-12-23T22:00:20.417

@El'endiaStarman Answers from the other challenge can be copied verbatim and be valid for this challenge, making this a duplicate. – Alex A. – 2015-12-23T22:56:28.860

1So it's not encoded whether the string starts with 0 or 1? – xnor – 2015-12-24T22:07:33.810

For the purposes of this challenge, no. @xnor – Zach Gates – 2015-12-24T23:16:36.993

Answers

2

Pyth - 5 bytes

hMrz8

Test Suite.

Maltysen

Posted 2015-12-23T21:48:49.533

Reputation: 25 023

2

Ruby, 37 29 bytes

->s{s.gsub(/0+|1+/){$&.size}}

Test:

->s{s.gsub(/0+|1+/){$&.size}}["10001110101"]
=> "1331111"

daniero

Posted 2015-12-23T21:48:49.533

Reputation: 17 193

You can save a few bytes with global variable magic: ->s{s.gsub(/0+|1+/){$&.size}} – blutorange – 2015-12-25T00:00:27.243

Ah, of course! Neat, thanks :) – daniero – 2015-12-25T01:43:25.027

2

Stuck, 5 bytes

soT]y

Takes a binary string as input and outputs an array.

Explanation

soT]y

s       get input string
 o      run-level encoding
  T     zip
   ]    flatten
    y   pops the data values off

a spaghetto

Posted 2015-12-23T21:48:49.533

Reputation: 10 647

1

CJam, 6 bytes

le`s2%

Try it here.

geokavel

Posted 2015-12-23T21:48:49.533

Reputation: 6 352

1

Haskell, 39 bytes

import Data.List
(show.length=<<).group

Unfortunately group is in Data.List, so I need the import.

Usage example: (show.length=<<).group $ "101011101011000011" -> "11113111242".

nimi

Posted 2015-12-23T21:48:49.533

Reputation: 34 639