Skip to content

Instantly share code, notes, and snippets.

@oussama-ht
Forked from alk-sramond/test.py
Last active June 9, 2022 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oussama-ht/7a5db782c647e482e432303048bb2bc4 to your computer and use it in GitHub Desktop.
Save oussama-ht/7a5db782c647e482e432303048bb2bc4 to your computer and use it in GitHub Desktop.
def is_valid_barcode(value):
if not isinstance(value, str):
return False
if len(value) not in [8, 12, 13, 14, 17, 18]:
return False
if len(value) != 18:
number_zero_to_add = 18 - len(value)
value = '0' * number_zero_to_add + value
try:
value = int(value)
except :
return False
value = str(value)
sum = 0
digit = int(value[-1])
value = value[:-1:]
for interger in value[::2]:
sum += int(interger) * 3
for interger in value[1::2]:
sum += int(interger)
if check_degit(sum) == digit:
return True
return False
def check_degit(value):
result = (value % 10)
if result > 0:
result = 10 - result
return result
# encoding: utf-8
# The goal of the exercise is to write the function is_valid_barcode which:
# - returns True if the given parameter is:
# - a string
# - which is a valid GTIN, GSIN or SSCC (see https://www.gs1.org/services/how-calculate-check-digit-manually)
# - returns False in EVERY other case
# This function should never raise an exception
#
# Precisions on the barcode format:
# - GTINs are made of 8, 12, 13 or 14 digits, GSIN are 17 characters and SSCC are 18 characters long.
# - the last one is a check digit, i.e. it can be calculated from the others
# - if the calculated check digit differs from the actual check digit, then the string is considered as an invalid GTIN
def is_valid_barcode(value):
return False
test_cases = [
('6291041500213', True), # <--- example of the spec
('6291041500211', False), # <-- example with wrong check digit
('3124482010481', True),
('3124482010482', False),
('3124482010483', False),
('3124482010484', False),
('3124482010485', False),
('3124482010486', False),
('3124482010487', False),
('3124482010488', False),
('3124482010489', False),
('3124482010480', False),
('0167053164698', True),
('13033490913240', True),
('13033490913240.00', False),
('123456017450', True),
('12345670', True),
('000000000000000000000', False),
('00000000000000000000', False),
('0000000000000000000', False),
('000000000000000000', True),
('00000000000000000', True),
('0000000000000000', False),
('000000000000000', False),
('00000000000000', True),
('0000000000000', True),
('000000000000', True),
('00000000000', False),
('0000000000', False),
('000000000', False),
('00000000', True),
('0000000', False),
('000000', False),
('00000', False),
('0000', False),
('000', False),
('00', False),
('0', False),
('zerozerozerozero', False),
("I am not a GTIN!4", False),
('0000OOOO000000000', False),
('ßþéçíæL ĉĥâ®åCẗëR§ ΅œ’', False),
([0,0,0,0,0,0,0,0], False),
(3124482010481, False),
]
for test, expected in test_cases:
try:
result = is_valid_barcode(test)
except Exception as e:
print("Failed: %s -> %s" % (test, e))
raise
if result != expected:
print("Failed: %s -> expected %s got %s" % (test, expected, result))
break
else:
print("Success")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment