3
\$\begingroup\$

This question already has an answer here:

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).

\$\endgroup\$

marked as duplicate by Alex A. code-golf Dec 24 '15 at 23:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ There is an error in your first example's output! The 1 digit after the first B shouldn't be there. \$\endgroup\$ – danmcardle Dec 23 '15 at 21:53
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – El'endia Starman Dec 23 '15 at 22:00
  • \$\begingroup\$ @El'endiaStarman Answers from the other challenge can be copied verbatim and be valid for this challenge, making this a duplicate. \$\endgroup\$ – Alex A. Dec 23 '15 at 22:56
  • 1
    \$\begingroup\$ So it's not encoded whether the string starts with 0 or 1? \$\endgroup\$ – xnor Dec 24 '15 at 22:07
  • \$\begingroup\$ For the purposes of this challenge, no. @xnor \$\endgroup\$ – Zach Gates Dec 24 '15 at 23:16
2
\$\begingroup\$

Pyth - 5 bytes

hMrz8

Test Suite.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Ruby, 37 29 bytes

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

Test:

->s{s.gsub(/0+|1+/){$&.size}}["10001110101"]
=> "1331111"
\$\endgroup\$
  • \$\begingroup\$ You can save a few bytes with global variable magic: ->s{s.gsub(/0+|1+/){$&.size}} \$\endgroup\$ – blutorange Dec 25 '15 at 0:00
  • \$\begingroup\$ Ah, of course! Neat, thanks :) \$\endgroup\$ – daniero Dec 25 '15 at 1:43
1
\$\begingroup\$

CJam, 6 bytes

le`s2%

Try it here.

\$\endgroup\$
1
\$\begingroup\$

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".

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.