This page is still under construction.

Parts of this page are still being built. What you see may change.

Encoding Binary Numbers

Interview

Time limit1sMemory limit128 MB

Summary
Encode each binary string as run lengths followed by digits and print the result per line.
Level

Easy1 of 10

Topics
String, Implementation
Solved
No attempts yet

Problem

A binary number can be encoded into decimal digits by counting how many times each digit repeats in a row.

Read the binary number from the left and cut it into the longest possible runs of one repeated digit. Write each run as its length followed by the digit that repeats, then join the pieces in order.

Binary numberCountingDecimal digits
0003x030
01111x0 3x11031
1110000013x1 5x0 1x1315011
10101x1 1x0 1x1 1x011101110

Input

Each line of the input holds a character string representing a binary number. The length of the binary number on each line is between 1 and 100 inclusive. A line that holds the single character 0 marks the end of the input, and that last line is not encoded. The input holds at most 1,000 lines.

Output

Print the decimal digits for each binary number, one per line. There is no space between digits.

Examples2

  1. Example 1

    Input
    111100000011
    000
    01010101
    11
    0
    
    Expected output
    416021
    30
    1011101110111011
    21
    
  2. Example 2

    Input
    000
    0111
    111000001
    1010
    0
    
    Expected output
    30
    1031
    315011
    11101110