Challenges

# Run-length encode a byte sequence

+2
−0

Run-length encoding is a simple compression technique which compresses sequences of repeating identical bytes.

The encoding rules for this task are as follows:

Any sequence of $n$ identical bytes ($3 \leq n \leq 63$) is replaced by a byte with value n+0xc0 followed by only one copy of that byte. If any of the original bytes has value 0xc0 or above, such encoding also happens if that byte occurs only one or two times in a row (thus the encoded sequence may actually be longer if such bytes occur).

The task is to write a run-length encoder. The input is a sequence of bytes. It may be a sequence of actual bytes, or a sequence of numbers in the range 0 to 255 (inclusive), whichever is more convenient. The output is again a sequence of bytes (again, in any suitable form), which is the proper run-length encoding of the input sequence.

Examples (byte values are given in 2-digit hex):

00 01 02 03 04        → 00 01 02 03 04
00 00 00 01 01 00     → c3 00 01 01 00
be bf c0 c1 c2        → be bf c1 c0 c1 c1 c1 c2
00 00 00 … (63 bytes) → ff 00
00 00 00 … (64 bytes) → ff 00 00
00 00 00 … (65 bytes) → ff 00 00 00
00 00 00 … (66 bytes) → ff 00 c3 00
ff ff ff … (63 bytes) → ff ff
ff ff ff … (64 bytes) → ff ff c1 ff
ff ff ff … (65 bytes) → ff ff c2 ff
ff ff ff … (66 bytes) → ff ff c3 ff


This is a code-golf challenge; the shortest program for a language wins.

Why does this post require moderator attention?
Why should this post be closed?

+2
−0

# JavaScript (Node.js), 101 82 bytes

-19 bytes thanks to Shaggy!

x=>x.replace(/(..)\1{0,62}/g,(c,g)=>c>'c'||c?(192+c.length/2).toString(16)+g:c)


Try it online!

Everything can be solved with regexes.

Takes input and outputs as hex strings.

Why does this post require moderator attention?

82 bytes (1 comment)
Save 3 bytes (1 comment)
+2
−0

# JavaScript (Node.js), 146 bytes

f=(a,c=n=~(b=d=[],e=x=>x&&(c>191|d>2?b.push(d+192,c):b.push(...d+n?[c,c]:[c]),c=n,d=0)))=>(a.map(b=>c^n?c=(e(d>62|b-c),d++,b):(c=b,d=1)),e(c-n),b)


Try it online!

Ungolfed version

Why does this post require moderator attention? 