繁体   English   中英

ISBN 校验位项目查找第 10 位数字

[英]ISBN Check Digits Project Finding 10th Digit

我被指派创建一个函数,该函数接受 9 个数字并返回第 10 个数字,就好像它是一个 ISBN 数字。 我将在底部包含确切的方向,但总而言之,最后一位数字必须是其他 9 位数字的总和,第一个数字乘以 1,第二个数字乘以 2,依此类推。

这是我到目前为止所拥有的:

def isbn_gendigit(isbn):
    '''
    docstring (fill in later)
    '''
    length = 9
    last_digit = 0
    last_digit = int(isbn[0]) * (length)
    for i in range(len(isbn)):
        length - 1
    isbn = isbn % 11
    last_digit = 11 - isbn
    print(ISBN + last_digit)
    return None

我知道它远未完成和完美,但我在理解如何解决问题时遇到了麻烦。 我在这里查看了其他类似的基于 ISBN 的问题,但我看到的少数问题比我正在寻找的要复杂得多。


项目方向:ISBN 校验位

国际标准书号 (ISBN) 是(2007 年之前)一个 10 位代码,用于唯一指定一本书。 最右边的数字是一个校验和数字,可以根据d1 + 2d2 + 3d3 + ... + 10d10 必须是11 的倍数的条件从其他9 个数字中唯一确定(这里di 表示从右边开始的第i 个数字)。

示例:对应于 020131452d10 的校验和数字是 d10 的唯一值在 0 和 10 之间——即:1*0 + 2*2 + 3*0 + 4*1 + 5*3 + 6*1 + 7 *4 + 8*5 + 9*2 + 10*d10 是 11 的倍数。

计算 d10:求 1*0 + 2*2 + 3*0 + 4*1 + 5*3 + 6*1 + 7*4 + 8*5 + 9*2) = 115 然后找到 115% 的余数11 = 5 这是第 10 位 d10 = 5 的值。因此,第 10 位 ISBN 现在是 0201314525 - 我们可以检查并看到 (115+10*5)%11 = 0 为真。

使用函数设计方法,定义一个 Python 函数 isbn_gendigit,带有一个参数 isbn,一个 9 位字符串。 该函数应计算给定 ISBN 的校验位,然后打印出 10 位 ISBN。 isbn_gendigit 将返回值 None (函数结束,键入“return None”)。

在以下示例上测试您的函数:

isbn_gendigit('020131452')

0201314525

isbn_gendigit('068131921')

0681319216


你得到了问题中的算法,这很好:

计算 d10:找到 (1*0 + 2*2 + 3*0 + 4*1 + 5*3 + 6*1 + 7*4 + 8*5 + 9*2) = 115 然后找到 115 的余数%11 = 5 即第 10 位的值 d10 = 5

您所要做的就是将其转换为 Python。

简单的for循环

您的解决方案没有正确实现算法。 length在做什么? 您所要做的就是从字符串的开头(左侧)迭代到结尾(右侧),同时跟踪字符串中的索引:

def isbn_gendigit(isbn):
    total = 0
    for i in range(len(isbn)): # iterates from 0 to the length of the string (minus 1)
        n = isbn[i]            # the ith character in the string
        num = int(n)           # convert to an integer
        total += (i+1) * num   # sum the digit times the increment
    d10 = total % 11           # modulo 11
    print(isbn + str(d10))     # print the result
    return None                # return None (technically not needed)

遍历字符串

我们可以做得更好。 首先, Python 中的字符串是可迭代的 为什么这很重要? 因为,根据规范,我们得到了一个字符串:

...定义一个 Python 函数 isbn_gendigit,带有一个参数 isbn,一个 9 位字符串。

这使得对输入值的迭代变得轻而易举,但这确实意味着我们必须将索引作为单独的变量i跟踪到字符串中:

def isbn_gendigit(isbn):
    total = 0
    i = 0
    for n in isbn:         # iterating over the string gives each character of the string in order
        num = int(n)       # convert to an integer
        i += 1             # increment the digit index
        total += i * num   # sum the digit times the increment
    d10 = total % 11       # modulo 11
    print(isbn + str(d10)) # print the result
    return None            # return None (technically not needed)

枚举字符串

我们仍然可以做得更好。 事实上,我们可以通过使用 Python 的内置enumerate函数进行迭代来获取数字及其在字符串中的位置,从而无需手动跟踪索引i

def isbn_gendigit(isbn):
    total = 0
    for i, n in enumerate(isbn): # now I have a number and a character
        num = int(n)             # convert to a number
        total += (i+1) * num     # sum the digits
    d10 = total % 11             # modulo 11
    print(isbn + str(d10))       # print the result
    return None                  # return None (technically not needed)

生成器表达式

当然,我们可以做得比这更好。 使用生成器表达式内置sum函数,我们可以将这个 for 循环 sum 变成一个单行:

def isbn_gendigit(isbn):
    # multiply and sum everything at once
    total = sum((i+1) * int(n) for i, n in enumerate(isbn))
    d10 = total % 11        # modulo 11
    print(isbn + str(d10))  # print the result
    return None             # return None (technically not needed)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM