“Sage code: ISBN code of the book” (4_6.sage) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def validIsbn(list,length):
    c=11
    for i in range(0,length):
        c=c-(length+1-i)*list[i]

    c1=mod(c,11)
    print 'One obtains c1=',c1, 'hence the ISBN code is: ',
    for j in range(0,length):
        if ((mod(j,3)==0)&(j!=0)):
            print '-',
        print list[j],
    print '-',c1

validIsbn([1,1,1,8,8,8,1,4,4],9)
> One obtains c1= 3 hence the ISBN code is:  1 1 1 - 8 8 8 - 1 4 4 - 3

def eanCode(list):
    c=10;a=0;b=0
    for i in range(0,6):
        a=a+list[2*i]
    print a
    for i in range(1,7):
        b=b+list[2*i-1]
    print b

    c0=mod(10-(a+3*b),10)
    print 'The EAN-13 code is: ',
    for i in range(0,12):
        if ((i==1)|(i==7)):
            print '-',
        print list[i],
    print c0

eanCode([9,7,8,1,1,1,8,8,8,1,4,4])
> 38
> 22
> The EAN-13 code is:  9 - 7 8 1 1 1 8 - 8 8 1 4 4 6