Split an Integer into Digits to Compute an Isbn Checksum

Split an integer into digits to compute an ISBN checksum

Just create a string out of it.

myinteger = 212345
number_string = str(myinteger)

That's enough. Now you can iterate over it:

for ch in number_string:
print ch # will print each digit in order

Or you can slice it:

print number_string[:2] # first two digits
print number_string[-3:] # last three digits
print number_string[3] # forth digit

Or better, don't convert the user's input to an integer (the user types a string)

isbn = raw_input()
for pos, ch in enumerate(reversed(isbn)):
print "%d * %d is %d" % pos + 2, int(ch), int(ch) * (pos + 2)

For more information read a tutorial.

How to Perform operations on a single digit in a whole number

You can convert the number to a string and then iterate over each character (digit):

def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)

num = 248
for digit in str(num):
print(factorial(int(digit)))

You can't iterate over a number, but iterating over a string yields its individual characters. I had to convert the string version of the number's digits back into an integer for the factorial function to work.

Without using loops, read the first 9 digits of an ISBN-10 as a string and calculate the 10th digit

std::string is an array of characters not integers. and in c++ '1' is not equal to 1.

What you are doing is adding characters ascii codes. what you need to do is change d10 calculation like this :

int d10 = ( (ISBN[0] - '0') * 1 + (ISBN[1] - '0') * 2 + (ISBN[2] - '0') * 3 + (ISBN[3] - '0') * 4 + (ISBN[4] - '0') * 5
+ (ISBN[5] - '0') * 6 + (ISBN[6] - '0') * 7 + (ISBN[7] - '0') * 8 + (ISBN[8] - '0') * 9 ) % 11;

To convert character to actual integer value (I mean '1' -> 1) you need to do something like this :

char a = '1';
int ia = a - '0'; //ia == 1

Split day-of-month integers into digits, sum them repeatedly until get a single-digit integer

dayStringSum is an integer, so dayStringSum[n] makes no sense. You'll want to turn it into a string first, and then look at its individual characters.

Also, you do not assign a new value to dayStringSum inside the while loop, so if it is >= 10 upon entering the loop, it will remain so, resulting in an infinite loop. You say that you got a final result of 4, but I fail to see how you would get a final result at all.

Try something like this:

daySum = int(day)  # Ensure that day is an int before we start.

while(daySum >= 10):
newString = str(daySum)
dayIntA = int(newString[0])
dayIntB = int(newString[1])
daySum = dayIntA + dayIntB # Will be checked on next iteration.

print(daySum)

Is there a built-in Python function or module that will sum digits multiplied by their integer weight?

No, that is way too specific to be something in a standard library. It is fairly simple to write though:

def check_isbn(isbn):
isbn_digits = (
10 if ch == 'X' else int(ch)
for ch in isbn
if ch.isdigit() or ch == 'X')

checksum = sum(
(10 - index) * digit
for index, digit
in enumerate(isbn_digits)
) % 11

return checksum % 11 == 0

I like the comprehension way; but the imperative way should also work, you just have some logic errors. In particular, you don't want to iterate all weights for each digit. Each character has exactly one weight if it's a digit, or none if it is not. So an imperative rewrite would be:

def check_isbn(isbn):
checksum = 0
weight = 10
for ch in isbn:
if ch == 'X':
digit = 10
elif ch.isdigit():
digit = int(ch)
else:
continue
checksum += digit * weight
weight -= 1

return checksum % 11 == 0

EDIT: various bugs

Why is my code outputting 0000000000000 as an invalid ISBN number?

The key statement is:

If this calculation results in an apparent check digit of 10, the check digit is 0

from your comment NOT your original question.

So if the check digit comes out to be 10 you actually need to use 0 in your comparison.

Calculate your check number then:

k = 0 if k == 10 else k

Calculating check digit for ISBN

You can use the mod(%) and divide(/) operator.

N%10 would give you the last digit.
N/10 (integer division) will remove the last digit.

You can continue till you have no more digits.

Putting check digit in to form ISBN 10

I am assuming that your ISBN13 is a string and when adding the check digit back to the SBN number, you would not want to affect the 9 digit number. I would suggest:

/* SBNString = 034096139 */
NSString *ISBN10 = [NSString stringWithFormat:@"%@%u", SBNString, checkDigit];

How do I separate a 2 digit number into individual digits?

Start by dividing the number by ten, there you have the first number.

int i = 99;
int oneNumber = i / 10;

You really should try to get the next one by yourself.



Related Topics



Leave a reply



Submit