Here at NML, one of our senior developers (and chief instigators of office humour), Charl, likes to keep the team on its toes with a regular coding challenge. They’ve become so popular – with prizes and instant office-cred involved – we’ve dubbed it the NML Charllenge.
In the spirit of open-source inspiration, here’s the latest one. If you’d like to tackle it, email your solution to us at info@newmedialabs.co.za with the subject line: Coding Challenge.
—
The Question
Write a Run-Length encoding algorithm to encode a piece of text and be able to decode the result back to the original.
You can use any language of your choice, and send as many versions as you want. Your submission will be evaluated on the following:
- Correct output for encoding and decoding (non-negotiable)
- Readability
- Elegance
- Brevity
- Inventiveness
To recap, a run-length encoder takes a stream and encodes it by counting repeating bytes/characters and then outputting it as the number of the byte/characters that was repeated, plus the byte/character. So “aabbcde” will be “2a2b1c1d1e”.
Below is the runtime encode string you should be able to decode. A special prize for the first person to send in the decode text and tell us the name of the author. (We’ll know if you cheated by trying to figure out the latter before the former.)
(Note that the results of the any encode should be able to serve as the input for the next decode and that again for the next encode, without any loss of fidelity.)
Good luck!
1`1T1w1a1s1 1b1r1i2l1i1g1,1 1a1n1d1 1t1h1e1 1s1l1i1t1h1y1 1t1o1v1e1s2 1D1i1d1 1g1y1r1e1
1a1n1d1 1g1i1m1b1l1e1 1i1n1 1t1h1e1 1w1a1b1e1:1A2l1 1m1i1m1s1y1 1w1e1r1e1 1t1h1e1
1b1o1r1o1g1o1v1e1s1,2 1A1n1d1 1t1h1e1 1m1o1m1e1 1r1a1t1h1s1 o1u1t1g1r1a1b1e1.2n1’1B1e1w1a1r1e1
1t1h1e1 1J1a2b1e1r1w1o1c1k1,1 1m1y1 1s1o1n1!2 1T1h1e1 1j1a1w1s1 1t1h1a1t1 1b1i1t1e1,1 1t1h1e1
1c1l1a1w1s1 1t1h1a1t1 1c1a1t1c1h1!1B1e1w1a1r1e1 1t1h1e1 1J1u1b1j1u1b1 1b1i1r1d1,1 1a1n1d1
1s1h1u1n2 1T1h1e1 1f1r1u1m1i1o1u1s1 1B1a1n1d1e1r1s1n1a1t1c1h1!1’2n1H1e1 1t2o1k1 1h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