有关奇偶校验的更多信息:维基百科
编写一个程序,它接受一个输入(stdin,argv,等)的四个比特和两个奇偶校验。然后,您的程序应该使用偶数奇偶校验测试“块”是否有效;如果是,则输出1。然后尝试作出任何更正,如果可能的纠正输出校正块。如果无法更正块输出-1。您可以假设奇偶校验不会损坏。
重新规定并澄清:
1。-1。输入格式:
<NIBBLE> <NIBBLE> <NIBBLE> <NIBBLE> <PARITY-Y> <PARITY-X>因此,1010 0111 1101 1011 1011 0111看起来应该是:

这是密码-高尔夫:最小的程序获胜。
输入1010 0111 1101 1011 1011 0111
输出1
输入是有效的
输入1010 0011 1101 1011 1011 0111
输出1010 0111 1101 1011
输入无效,但可纠正。
输入1010 0001 1101 1011 1011 0111
输出-1输入无效,不可纠正
发布于 2014-05-29 18:53:46
不过,我觉得有点长。请提出改进或任何解决办法..。
i=raw_input()
z="""#h(a?(a&1)^h(a/2)~a@0
#y(i,m,l,c=0?h(i&m)|y(i>>l,m,l,c+1)*2~c<4@0
#p(i?y(i,4369,1)*16+y(i,15,4)
k=i.replace(" ","")
m=int(k[:16],2)
c=int(k[-8:],2)
#s(i?s(i>>4)+" "+bin(i&15^16)[3:]~i@""
#q(j=16):d=m^1<<j;!s(d)[1:]~p(d)==c@q(j-1)~j@-1
#f(?1~p(m)==c@q()"""
for k,l in("?","):!"),("!","return "),("@"," else "),("#","def "),("~","if "):z=z.replace(k,l)
exec z
print f()稍微没有高尔夫球..。
def y(i,m,l,c=0):
h=lambda a:(a&1)^h(a/2)if a else 0 # Calculate how many set bits in i
return h(i&m)|y(i>>l,m,l,c+1)*2if c<4 else 0 # Sets the parity bits accordingly recursively
p=lambda i:y(i,4369,1)*16+y(i,15,4) # Returns the y and x parity together
i="1010 0111 1101 1011 1011 0111"
k=i.replace(" ","") # Remove all spaces
m=int(k[:16],2) # Extracts the matrix
c=int(k[-8:],2) # Extracts the parities
s=lambda i:s(i>>4)+" "+bin(i&15^16)[3:]if i else""
# Converts from int to space separated hexas in binary
def f(i):
if p(m)==c:return 1 # If calculated parities matches the test
for i in range(16): # Else, for each of the 16 bits in the matrix
d=m^1<<i # xor it
if p(d)==c:return s(d)[1:] # return the matrix if its parities matches the test
return -1 # If all else fails, return -1
print f(i)发布于 2016-04-28 00:09:14
s=>(a=s.split` `,[...a.pop()].map((c,i)=>a[i]+=c),x=y=0,[...a].map((s,i)=>[...s].map((c,j)=>{x^=c<<i;y^=c<<j})),x&=15,y&=15,a.pop(),x|y?x-(x&-x)|y-(y&-y)?-1:[...a].map((s,i)=>s.slice(0,4).replace(/./g,(c,j)=>c^x>>i&y>>j&1)).join` `:1)如果字符串数组是可接受的I/O格式,则保存21个字节;如果二维位数组可以接受,则保存46个字节。说明:原来的字符串被分割成块,然后重新排列成一个几乎正方形的形状:
DDDDX
DDEDX
DDDDX
DDDDX
YYYY然后计算行和列参数,每个参数在x和y变量中设置适当的位,因此,例如,如果错误在标记为E的位中,它将将x设置为2,将y设置为4。(如果还提供了奇偶校验块的奇偶校验,那么也可以检查奇偶校验块或奇偶校验比特本身中的错误。当然,两个奇偶校验块应该具有相同的奇偶性。)
s=>( String parameter
a=s.split` `, Split it into blocks
[...a.pop()] Remove the X parity block and loop over it
.map((c,i)=>a[i]+=c), Append the X parity bits to the data blocks
x=y=0, Initialise the X and Y parity
[...a].map((s,i)=> Loop over rows
[...s].map((c,j)=> and columns
{x^=c<<i;y^=c<<j})), updating the parity
x&=15,y&=15, Only bits 0-3 are important
a.pop(), Remove the Y parity block
x|y? If there were parity errors
x-(x&-x)|y-(y&-y)?-1: If there were several errors then return -1
[...a].map((s,i)=> If there was one error then loop over blocks
s.slice(0,4) Removing the X parity bit
.replace(/./g,(c,j)=> Loop over bits
c^x>>i&y>>j&1)) Flip the bit if it was the incorrect bit
.join` `: Join the blocks together
1) Return 1 if there were no parity errorshttps://codegolf.stackexchange.com/questions/28724
复制相似问题