同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""https://en.wikipedia.org/wiki/Check_digit#Algorithms"""def get_check_digit(barcode: int) -> int:"""Returns the last digit of barcode by excluding the last digit firstand then computing to reach the actual last digit from the remaining12 digits.>>> get_check_digit(8718452538119)9>>> get_check_digit(87184523)5>>> get_check_digit(87193425381086)9>>> [get_check_digit(x) for x in range(0, 100, 10)][0, 7, 4, 1, 8, 5, 2, 9, 6, 3]"""barcode //= 10 # exclude the last digitchecker = Falses = 0# extract and check each digitwhile barcode != 0:mult = 1 if checker else 3s += mult * (barcode % 10)barcode //= 10checker = not checkerreturn (10 - (s % 10)) % 10def is_valid(barcode: int) -> bool:"""Checks for length of barcode and last-digitReturns boolean value of validity of barcode>>> is_valid(8718452538119)True>>> is_valid(87184525)False>>> is_valid(87193425381089)False>>> is_valid(0)False>>> is_valid(dwefgiweuf)Traceback (most recent call last):...NameError: name 'dwefgiweuf' is not defined"""return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10def get_barcode(barcode: str) -> int:"""Returns the barcode as an integer>>> get_barcode("8718452538119")8718452538119>>> get_barcode("dwefgiweuf")Traceback (most recent call last):...ValueError: Barcode 'dwefgiweuf' has alphabetic characters."""if str(barcode).isalpha():msg = f"Barcode '{barcode}' has alphabetic characters."raise ValueError(msg)elif int(barcode) < 0:raise ValueError("The entered barcode has a negative value. Try again.")else:return int(barcode)if __name__ == "__main__":import doctestdoctest.testmod()"""Enter a barcode."""barcode = get_barcode(input("Barcode: ").strip())if is_valid(barcode):print(f"'{barcode}' is a valid barcode.")else:print(f"'{barcode}' is NOT a valid barcode.")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。