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