Skip to content

Instantly share code, notes, and snippets.

@alk-sramond
Last active June 15, 2022 13:55
Show Gist options
  • Save alk-sramond/6da89a93800aa5f2b5ae2c17b1106603 to your computer and use it in GitHub Desktop.
Save alk-sramond/6da89a93800aa5f2b5ae2c17b1106603 to your computer and use it in GitHub Desktop.
# 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:
assert expected == is_valid_barcode(test)
print("Success")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment