print "(3,4) parity check code."
def parity_checker(word):
count=0#Initially the number of 1s is zero
for i in range(0,4):#Ranges till 4 as the size of the received word is 4
if word[i]!='0':
count=count+1#Counts the number of 1s in the given code
if count%2==0:#Even weight can only be detected by this checker
return "No, error cannot be detected"
else:
return "Yes, error can be detected"
print "0010-",parity_checker(str('0010'))
print "1001-",parity_checker(str('1001'))
print "(6,7) parity check code."
def parity_checker(word):
count=0#Initially the number of 1s is zero
for i in range(0,7):#Ranges till 7 as the received word consists of 7 digits
if word[i]!='0':
count=count+1#Counts the number of 1s in the given code
if count%2==0:#Even weight can only be detected by this checker
return "No, error cannot be detected"
else:
return "Yes, error can be detected"
print "1101010-",parity_checker(str('1101010'))
print "1010011-",parity_checker(str('1010011'))
print "0011111-",parity_checker(str('0011111'))
print "1001101-",parity_checker(str('1001101'))
print "Consider (2,6) encoding function"
encode=["00","01","10","11"]#Set of data to be encoded
k=0#Value for filling the distance is initially set to zero
distance=["" for i in range(7)]#Array that can hold all possible combinations of distance value ie. 6 combinations
def e(word):#Function that returns the encoded value
if word=="00":
return str("000000")
elif word=="10":
return str("101010")
elif word=="01":
return str("011110")
elif word=="11":
return str("111000")
def d(word1,word2):
count=0#Variable that counts the number of 1s
data1=str(e(word1))
data2=str(e(word2))#Convert into string for indexing which facilitates bitwise operation
for i in range(0,6):
if data1[i]!=data2[i]:
count=count+1#XOR implementation
return count
for i in range(0,4):
for j in range(i+1,4):
distance[k]=d(encode[i],encode[j])
print "d(e(",encode[i],"),e(",encode[j],"))=",distance[k]
k=k+1
print "Minimum distance is",min(distance)#Finds the minimum distance
print "Since the minimum distance is",min(distance),"the code will detect",(min(distance)-1),"or fewer errors"
print "Consider (3,9) encoding function"
encode=["000","001","010","011","100","101","110","111"]#Set of data to be encoded
k=0#Value for filling the distance is initially set to zero
distance=["" for i in range(28)]#Array that can hold all possible combinations of distance value ie. 6 combinations
def e(word):#Function that returns the encoded value
if word=="000":
return str("000000000")
elif word=="001":
return str("011100101")
elif word=="010":
return str("010101000")
elif word=="011":
return str("110010001")
elif word=="100":
return str("010011010")
elif word=="101":
return str("111101011")
elif word=="110":
return str("001011000")
elif word=="111":
return str("110000111")
def d(word1,word2):
count=0#Variable that counts the number of 1s
data1=str(e(word1))
data2=str(e(word2))#Convert into string for indexing which facilitates bitwise operation
for i in range(0,9):#Since it is a (3,9) encoder
if data1[i]!=data2[i]:
count=count+1#XOR implementation
return count
for i in range(0,8):#Since there are eight possibilities with three digits
for j in range(i+1,8):
distance[k]=d(encode[i],encode[j])
print "d(e(",encode[i],"),e(",encode[j],"))=",distance[k]
k=k+1
print "Minimum distance is",min(distance)#Finds the minimum distance
print "Since the minimum distance is",min(distance),"the code will detect",(min(distance)-1),"or fewer errors"
print "To prove the given set to be a group code"
print "Since 00000 belongs to the set, identity is satisfied"
result=["" for i in range(5)]#List that holds the result of xor
word=['00000','10101','01110','11011']#Initial set of encoded words
count=0#Variable that counts the number of pairs that satisfies the closure property
for i in range(0,4):
for j in range(i+1,4):#Possible combinations of words
data1=word[i]
data2=word[j]
print "\n"
print data1,"exor",data2,"=",
for k in range(0,5):#XOR gate operations
if data1[k]!=data2[k]:
result[k]=1
print result[k],
else:
result[k]=0
print result[k],
result1=''.join(str(e) for e in result)#Converts list to string for comparing purpose
for r in range(0,4):#Checks if it belongs to the given set of words
if result1==word[r]:
count=count+1
if count==6:#Since there are 6 possible pairs of words
print "\nClosure property is satisfied as we found that if x and y belongs to the set, then x xor y also belongs to the set."
else:
print "Closure property is not satisfied"
print "Associativity can be satisfies and also each element has an inverse."
print "Hence, it is a group code"