awa试了好久才出来的
查看原帖
awa试了好久才出来的
1205503
Aurzex楼主2023/12/2 15:12
# Aurzex's Python Code
def validate_isbn(isbn):
    # Remove hyphens from the ISBN
    isbn_digits = [int(digit) for digit in isbn if digit.isdigit()]
    # Calculate the checksum
    checksum = sum((i + 1) * digit for i, digit in enumerate(isbn_digits[:-1])) % 11
    # Check if the calculated checksum matches the last digit of the ISBN
    if checksum == 10:
        correct_checksum = 'X'
    else:
        correct_checksum = str(checksum)
 # Aurzex, Senior Programmer

def validate_isbn(isbn):
  # Remove hyphens from the ISBN
  isbn_digits = [int(digit) for digit in isbn if digit.isdigit()]

  # Calculate the identification code
  identification_code = sum((i + 1) * digit for i, digit in enumerate(isbn_digits[:9])) % 11

  # Check if the identification code is correct
  if identification_code == 10:
    correct_code = 'X'
  else:
    correct_code = str(identification_code)

  # Compare the calculated code with the provided code
  if isbn[-1] == correct_code:
    return "Right"
  else:
    # Replace the incorrect identification code in the ISBN
    corrected_isbn = isbn[:-1] + correct_code
    return corrected_isbn

print(validate_isbn(input()))
2023/12/2 15:12
加载中...