【每日一题20221010】ISBN-10校验

:mage: ISBN-10标识符的长度为十位数。前九个字符是数字0-9。最后一个数字可以是0-9或X,表示值为10。

如果数字之和乘以其位置模11等于零,则ISBN-10数字有效。

例如:

ISBN     : 1 1 1 2 2 2 3 3 3  9
position : 1 2 3 4 5 6 7 8 9 10

它是一个合法的ISBN-10 :

(1*1 + 1*2 + 1*3 + 2*4 + 2*5 + 2*6 + 3*7 + 3*8 + 3*9 + 9*10) % 11 = 0

例子

1112223339   -->  true
111222333    -->  false
1112223339X  -->  false
1234554321   -->  true
1234512345   -->  false
048665088X   -->  true
X123456788   -->  false

题目难度:简单
题目来源: ISBN-10 Validation | Codewars

def valid_ISBN10(isbn: str) -> bool:
    # your code here

assert valid_ISBN10('1112223339') ==True
assert valid_ISBN10('048665088X') == True
assert valid_ISBN10('1293000000') == True
assert valid_ISBN10('1234554321') == True
assert valid_ISBN10('1234512345') == False
assert valid_ISBN10('1293') == False
assert valid_ISBN10('X123456788') == False
assert valid_ISBN10('ABCDEFGHIJ') == False
assert valid_ISBN10('XXXXXXXXXX') == False
assert valid_ISBN10('123456789T') == False
assert valid_ISBN10('048665088XX') == False
1 个赞
def valid_ISBN10(isbn: str) -> bool:
    # your code here
    tmp_str = '0123456789X'
    result = False
    count = 0
    k = 1
    if set(isbn).issubset(set(tmp_str)) and (l := len(isbn)) == 10:
        if 'X' in isbn and isbn.index('X') == l - 1:
            count = 100
            for i in isbn[:-1]:
                count += int(i) * k
                k += 1
            if count % 11 == 0:
                result = True
        elif 'X' not in isbn:
            for i in isbn:
                count += int(i) * k
                k += 1
            if count % 11 == 0:
                result = True
    return result

solution=lambda isbn:False if sum([10*j if I==“X” else int(i)*j for I,j in zip(isbn,range(1,len(isbn)+1))])%11 else True

def valid_ISBN10(isbn: str) -> bool:
    list_result,n=[],0
    list_result.extend(isbn)
    if len(list_result)<10:return False
    try:
        if list_result[-1] == 'X':list_result[-1] = 10
        for i in range(len(list_result)+1):
            z=i*int(list_result[i-1])
            n+=z
        if n % 11 == 0:return True
        else:return False   
    except ValueError:
        return False
def valid_ISBN10(isbn: str) -> bool:
    if len(isbn)!=10:return False
    list_result, n = [], 0
    list_result.extend(isbn)
    if list_result[-1] == 'X': list_result[-1] = 10
    for index, item in enumerate(list_result, start=1):# 索引修成从 1 开始
        try:
            n=n+index*int(item)
        except ValueError:
                return False
    return n%11==0
2 个赞
def valid_ISBN10(isbn: str) -> bool:
    sum = 0
    if len(isbn) == 10:
        for i in range(9):
            if isbn[i] in '0123456789':
                sum += int(isbn[i]) * (i + 1)
            else:
                return False
        if isbn[-1] == 'X':
            sum += 10 * 10
        elif isbn[-1] in '0123456789':
            sum += int(isbn[-1]) * 10
        else:
            return False
    else:
        return False
    if sum % 11 == 0:
        return True
    else:
        return False
def valid_ISBN10(isbn): 
    if len(isbn)!=10:return False
    sum=0
    for index,value in enumerate(isbn,start=1):
        if index != len(isbn) and value not  in set("0123456789") or (index == len(isbn) and value not in set("0123456789X")) :
            return False
        if index == len(isbn) and value == 'X':
            value=10
        sum += index * int(value)
    return sum%11==0
2 个赞
def valid_ISBN10(isbn: str) -> bool:
    if not isbn[:-1].isdigit() or len(isbn) != 10 or (not (isbn[-1] == 'X' or isbn[-1].isdigit())):
        return False
    sum = 0
    for index in range(len(isbn)):
        if (index == len(isbn) - 1) and (isbn[-1] == 'X'):
            sum += (index + 1) * 10
        else:
            sum += (index + 1) * int(isbn[index])
    return sum % 11 == 0
1 个赞

这里对于第10位的判断是不完善的,只判断了第10位是0~9或X的情况,如果这一位是其它值,可能会输出错误的结果。如:
assert valid_ISBN10(‘129300000T’) == False

:+1:
修改了

from functools import reduce
def valid_ISBN10(isbn: str) -> bool:
    if len(isbn)==10 and isbn[0:8].isnumeric():
        if isbn[9]=="X":
            list_isbn = [int(list(isbn)[i]) * (i + 1) for i in range(9)]
            list_isbn.append(100)

        elif isbn[9].isnumeric():
            list_isbn=[int(list(isbn)[i])*(i+1) for i in range(len(isbn)) ]

        else:
            return False
    else:
        return False
    sum=reduce(lambda x,y:x+y,list_isbn)##累计求和
    return True if sum%11==0 else  False

assert valid_ISBN10('1112223339') ==True
assert valid_ISBN10('048665088X') == True
assert valid_ISBN10('1293000000') == True
assert valid_ISBN10('1234554321') == True
assert valid_ISBN10('1234512345') == False
assert valid_ISBN10('1293') == False
assert valid_ISBN10('X123456788') == False
assert valid_ISBN10('ABCDEFGHIJ') == False
assert valid_ISBN10('XXXXXXXXXX') == False
assert valid_ISBN10('123456789T') == False
assert valid_ISBN10('048665088XX') == False
def valid_ISBN10(isbn: str) -> bool:
    isbn_len = len(isbn)
    num_sum = 0
    if isbn_len != 10:  # 判断标识符的长度是否为十位数,不是则返回False
        return False
    else:               # 以下为标识符的长度为十位数的情况
        num_index = 1
        for num_value in isbn:
            if num_index < 10:  # 判断前九个字符是数字0-9,不是则返回False
                if num_value not in "0123456789":
                    return False
                    break
            if num_index == 10:  # 判断第十个数字是0-9或X,不是则返回False
                if num_value not in "0123456789X":
                    return False
                    break
                if num_value == "X":  # X表示值为10
                    num_value = 10
            num_sum += int(num_value) * num_index
            num_index += 1
    if num_sum % 11 == 0:  # 数字之和乘以其位置模11等于零,则ISBN-10数字有效,返回True,否则,返回False
        return True
    else:
        return False


assert valid_ISBN10('1112223339') == True
assert valid_ISBN10('048665088X') == True
assert valid_ISBN10('1293000000') == True
assert valid_ISBN10('1234554321') == True
assert valid_ISBN10('1234512345') == False
assert valid_ISBN10('1293') == False
assert valid_ISBN10('X123456788') == False
assert valid_ISBN10('ABCDEFGHIJ') == False
assert valid_ISBN10('XXXXXXXXXX') == False
assert valid_ISBN10('123456789T') == False
assert valid_ISBN10('048665088XX') == False

def valid_ISBN10(isbn: str) -> bool:
    if len(isbn)!=10:
        return False
    else:
        for i in range(len(isbn)-1):
            if not isbn[i].isdigit():
                return False
        if not isbn[-1].isdigit() and isbn[-1]!='X':
            return False
        res_list=[10 if isbn[i]=='X' else int(isbn[i]) for i in range(len(isbn))]
        if sum([(index+1)*value for index,value in enumerate(res_list)])%11==0:
            return True
        else:
            return False

assert valid_ISBN10('1112223339') ==True
assert valid_ISBN10('048665088X') == True
assert valid_ISBN10('1293000000') == True
assert valid_ISBN10('1234554321') == True
assert valid_ISBN10('1234512345') == False
assert valid_ISBN10('1293') == False
assert valid_ISBN10('X123456788') == False
assert valid_ISBN10('ABCDEFGHIJ') == False
assert valid_ISBN10('XXXXXXXXXX') == False
assert valid_ISBN10('123456789T') == False
assert valid_ISBN10('048665088XX') == False