ISBN-13 is a code (number) used to identify books. Each book has its own unique ISBN-13
It has a specific format, with the 13th digit (rightmost digit) being a validation digit
Here’s the algorithm for validating an ISBN:
sumodd (the leftmost digit is digit 1, odd)sumeven (the second leftmost digit is digit 2, even)The number \(9780306406157\) would be processed as follows:
Since 100 is divisible by 10, we know that this is a valid book number.
check_isbn13(isbn) – takes an integer (book number) as argument, and returns True if the isbn integer is a valid ISBN13make_isbn13(book_number) – takes an integer (book number) as argument, and returns an equivalent valid ISBN 13 (returning the new book number as an integer)You should probably write a helper function that given a book number it returns its total (\(total = sumodd + 3 ∗ sumeven\))
There are many ways to interate over each single digit in an integer, here’s one option:
str() function and then write a for i in range(len(string)): loop to get the index for each digitfor digit in digits(isbn): loopmake_isbn13(integer)make_isbn13(4) should return 42 because:
make_isbn13(7) should return should return 71 because:
You might be considering doing this:
(10 - total % 10) // 3
To make the number 12345 into a valid number, you need to add 1 to the end because:
The remainder of 27 divided by 10 is 7 and (10 - 7) // 3 = 1
But wait, this approach does NOT work for all cases
(10 - total % 10) // 3
To make the number 3 into a valid number, you need to add 9 because:
The remainder of 3 (total) divided by 10 is 3 and (10 - 3) // 3 = 2
But the last digit should be 9 instead.