Correct errors using Hamming(7,4)
Piet 50x11 = 550
codel size is 15. Not too concerned about size, but it passed all the tests.
Python, 79
f=lambda x,n=0,e=3:e&~-e and f(x,n+1,(n&8)*14^(n&4)*19^(n&2)*21^n%2*105^x)or~-n
Take input and output as numbers with the least significant bit on the right.
Instead of attempting error recovery, we just try encoding every possible message n
from 0 to 15 until we get an encoding that's one bit away from what's given. The recursion keeps incrementing n
until it finds one that works and returns it. Though there's no explicit termination, it must end within 16 loops.
The expression (n&8)*14^(n&4)*19^(n&2)*21^n%2*105
implements the Hamming matrix bitwise.
To check for a single error, we xor the given message with a computed one to get e
, and check if it's a power of two (or 0) with the classic bit-trick e&~-e==0
. But, we can't actually assign to the variable e
within a lambda, and we refer to it twice in this expression, so we do a hack of passing it as an optional argument to the next recursive step.
JavaScript (ES6), 92 87 81
Function getting and returning an integer in MSB.
The implementation is straightforwrd following @randomra comment:
- calc p3wrong|p2wrong|p1wrong (line 2,3,4)
- use it as a bit mask to flip the incorrect bit (line 1),
- then return just the data bits (last line)
F=w=>(w^=128>>(
(w^w*2^w*4^w/2)&4|
(w/8^w^w*2^w/16)&2|
(w/16^w/4^w^w/64)&1
))&7|w/2&8
Test In Frefox/FireBug console
;[0b1110000,0b1100000,0b1111011,0b0110001,
0b1011011,0b0101001,0b1010000,0b0100010]
.map(x=>x.toString(2)+'->'+F(x).toString(2))
Output
["1110000->1000", "1100000->1000", "1111011->1111", "110001->1011", "1011011->1010", "101001->1", "1010000->1000", "100010->10"]