Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Python 3.6.1 Most books now published are assigned a code which uniquely i

ID: 3817232 • Letter: U

Question

Using Python 3.6.1

Most books now published are assigned a code which uniquely identifies the book. The International Standard Book Number or ISBN, is normally a sequence of 10 decimal digits, but in some cases, the capital letter X may also appear as the lenth digit. lyphens are included at various places in the ISBN lo make them easier to read, but have no other significance. The sample input and expected output shown below illustrate many valid, and a few invalid, forms for ISBNs Actually, only the first nine digits in an ISBN are used to identify a book. The tenth character serves as a check digit to verify that the preceding 9 digits are correctly formed. This check digit is selected so that the value computed as shown in the following algorithm is evenly divisible by 11. Since the check digit may sometimes need to be as large as 10 to guarantee divisibility by 11, a special symbol was selected by the ISBN designers to represent 10, and that is the role played by X. The algorithm used to check an ISBN is relatively simple. Two sums, s1 and s2, are computed over the digits of the ISBN. s1 is the partial sum of the digits of ISBN and s2 the partial sum of s1 The ISBN is correct if the final value of s2 is evenly divisible by 11. Some writers use the term c lative sui instead of partial sum, but they both mean the same thing An example will clarify the procedure. Consider the (correct) ISBN 0-13-162959-X, First look at the calculation of sl digits in the ISBN 0 1 3 11 13 22 36 sl (partial sums) 46 The calculation of s2 is done by computing the total of the partial sums in the calculation of si: 3 10 21 34 56 83 119 165 s2 (partial sums of S We now verify the correctness of the ISBN by noting that 165 is indeed, evenly divisible by 11

Explanation / Answer

def extract_digits(string):
    res = []
    for i in string:
        if i.isnumeric():#if char is numeric put it in list
            res.append(int(i))
    return res

def isinvalid(string):

    if len(string) < 9: #if string size < 9 its invalid
        return False
    for i in range(len(string) - 1):
        if string[i] not in '0123456789-':
            return True#if intermediate string are other then 0-9 or - its invalid
    if string[-1] not in '0123456789xX':
        return True
    return False #last char can be 0-9 or x or X

def check_valid_ISBN(string):
  
    if isinvalid(string):
        return "invalid"
    numlist = extract_digits(string)
    #print (numlist)
    if len(numlist) < 9 or len(numlist) > 10:#if valid char size is not 9 or 10 its invalid
        return "invalid"
    if len(numlist) == 9:#if we have only 9 digits last one is 10
        numlist.append(10)
    for i in range(1, len(numlist)):
        numlist[i] += numlist[i - 1]#calculate partial sum
    #print (numlist)
    for i in range(1, len(numlist)):
        numlist[i] += numlist[i - 1]#calculate sum of partial sum
    #print (numlist)
    if numlist[9] % 11 == 0:#if total sum % 11 is 0 its valid ISBN
        return "valid"
    else:
        return "invalid"

#print(check_valid_ISBN("80-923701-0-9"))
#print (check_valid_ISBN("0-1315-2447-X"))
def main():
    readf = "isbn.txt"
    writef = "isbnOut.txt"
    target = open(writef, 'w')
    infile = open(readf, 'r')
    for line in infile:
        line = line[0:len(line) - 1]
        target.write(line)
        target.write(' ')
        target.write(check_valid_ISBN(line))
        target.write(' ')
    target.close()
    infile.close()
main()


input:isbn.txt
80-923701-0-9
80-923701-0-X
80-923701-0-x
80-923701-0-y
0-1315-2447-X
0-1315-2447-1

output:isbnOut.txt

80-923701-0-9   valid
80-923701-0-X   invalid
80-923701-0-x   invalid
80-923701-0-y   invalid
0-1315-2447-X   valid
0-1315-2447-1   invalid

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote