"""UPC Validator.""" """ -- Query to retrieve list of UPCS SELECT r.manufacturer_upc FROM releases r INNER JOIN distribution_format df ON df.distribution_format_id = r.distribution_format_id WHERE df.context_type = 'physical' AND r.manufacturer_upc IS NOT NULL AND r.manufacturer_upc != '' AND r.manufacturer_upc != r.display_upc; """ test_upcs = [ '796030114977', '190374000000', '190374000017', '190374000024', '190374000031', '190374000048', '190374000055', '190374000062', '190374000079', '190374000086', '190374000093', '234232425223', '5023001900421', '647587625345', '9781901447111' ] def index_is_odd(i): """Test oddness of index.""" return i % 2 == 1 def validate_upc(upc): """Validate UPC.""" total = 0 check_digit = int(upc[-1]) for i, digit in enumerate(upc[:-1][::-1]): total += (int(digit) if index_is_odd(i) else (int(digit) * 3)) total %= 10 calculated_check_digit = 10 - total if total else 0 return calculated_check_digit == check_digit def validate_list_of_upcs(upcs): """Validate list of upcs.""" return [upc for upc in upcs if not validate_upc(upc)] print(validate_list_of_upcs(test_upcs))