# 10 Groups and rings¶

## Example 09:Page 457¶

In :
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'))

(3,4) parity check code.
0010- Yes, error can be detected
1001- No, error cannot be detected


## Example 10:Page 457¶

In :
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'))

(6,7) parity check code.
1101010- No, error cannot be detected
1010011- No, error cannot be detected
0011111- Yes, error can be detected
1001101- No, error cannot be detected


## Example 11:Page 457¶

In :
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"


Consider (2,6) encoding function
d(e( 00 ),e( 01 ))= 4
d(e( 00 ),e( 10 ))= 3
d(e( 00 ),e( 11 ))= 3
d(e( 01 ),e( 10 ))= 3
d(e( 01 ),e( 11 ))= 3
d(e( 10 ),e( 11 ))= 2
Minimum distance is 2
Since the minimum distance is 2 the code will detect 1 or fewer errors


## Example 12:Page 458¶

In :
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"


Consider (3,9) encoding function
d(e( 000 ),e( 001 ))= 5
d(e( 000 ),e( 010 ))= 3
d(e( 000 ),e( 011 ))= 4
d(e( 000 ),e( 100 ))= 4
d(e( 000 ),e( 101 ))= 7
d(e( 000 ),e( 110 ))= 3
d(e( 000 ),e( 111 ))= 5
d(e( 001 ),e( 010 ))= 4
d(e( 001 ),e( 011 ))= 5
d(e( 001 ),e( 100 ))= 7
d(e( 001 ),e( 101 ))= 4
d(e( 001 ),e( 110 ))= 6
d(e( 001 ),e( 111 ))= 4
d(e( 010 ),e( 011 ))= 5
d(e( 010 ),e( 100 ))= 3
d(e( 010 ),e( 101 ))= 4
d(e( 010 ),e( 110 ))= 4
d(e( 010 ),e( 111 ))= 6
d(e( 011 ),e( 100 ))= 4
d(e( 011 ),e( 101 ))= 5
d(e( 011 ),e( 110 ))= 5
d(e( 011 ),e( 111 ))= 3
d(e( 100 ),e( 101 ))= 5
d(e( 100 ),e( 110 ))= 3
d(e( 100 ),e( 111 ))= 5
d(e( 101 ),e( 110 ))= 6
d(e( 101 ),e( 111 ))= 4
d(e( 110 ),e( 111 ))= 8
Minimum distance is 3
Since the minimum distance is 3 the code will detect 2 or fewer errors


## Example 13:Page 458¶

In :
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"

To prove the given set to be a group code
Since 00000 belongs to the set, identity is satisfied

00000 exor 10101 = 1 0 1 0 1

00000 exor 01110 = 0 1 1 1 0

00000 exor 11011 = 1 1 0 1 1

10101 exor 01110 = 1 1 0 1 1

10101 exor 11011 = 0 1 1 1 0

01110 exor 11011 = 1 0 1 0 1
Closure property is satisfied as we found that if x and y belongs to the set, then x xor y also belongs to the set.
Associativity can be satisfies and also each element has an inverse.
Hence, it is a group code