MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_check_digit

Function get_check_digit

strings/barcode_validator.py:6–32  ·  view source on GitHub ↗

Returns the last digit of barcode by excluding the last digit first and then computing to reach the actual last digit from the remaining 12 digits. >>> get_check_digit(8718452538119) 9 >>> get_check_digit(87184523) 5 >>> get_check_digit(87193425381086) 9 >>>

(barcode: int)

Source from the content-addressed store, hash-verified

4
5
6def get_check_digit(barcode: int) -> int:
7 """
8 Returns the last digit of barcode by excluding the last digit first
9 and then computing to reach the actual last digit from the remaining
10 12 digits.
11
12 >>> get_check_digit(8718452538119)
13 9
14 >>> get_check_digit(87184523)
15 5
16 >>> get_check_digit(87193425381086)
17 9
18 >>> [get_check_digit(x) for x in range(0, 100, 10)]
19 [0, 7, 4, 1, 8, 5, 2, 9, 6, 3]
20 """
21 barcode //= 10 # exclude the last digit
22 checker = False
23 s = 0
24
25 # extract and check each digit
26 while barcode != 0:
27 mult = 1 if checker else 3
28 s += mult * (barcode % 10)
29 barcode //= 10
30 checker = not checker
31
32 return (10 - (s % 10)) % 10
33
34
35def is_valid(barcode: int) -> bool:

Callers 1

is_validFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected