Barcode Scanner

A barcode scanner can be configured by scanning a series of barcodes in the correct order. Barcode configurations are encoded into a single string and stored as a blob in the backend system. The client requests the configuration from the backend system, and then needs to present the configurations in the correct order. The encoded configuration string is a series of pairs separated by |. The ordinal index valid is a 4 digit numeric prefixed with zeros. For example, the first configuration will be represented as 0001

The goals are to 1. Validate the configuration string and 2. provide the configuration client the configuration value in the order required to successfully configure the barcode scanner.

Validation conditions:

All configurations must be seperated by | character Configurations cannot skip a number in the ordering. If there are three configuraiton strings, there must be a 1, 2 and 3 index Configuration values are alphanumeric and may contain no other characters Configuration value length is exactly 10 characters Ordinal indices may not repeat, for example there cannot be two occurrences of number 1 Each configuration value is unique, configurations do not repeat. 0000 is not a valid ordinal index If a Configuration string is invalid return [“Invalid configuration”]

Example

configuration = “0002f7c22e7904|000176a3a4d214|000305d29f4a4b” Output: [ “76a3a4d214”, “f7c22e7904”, “05d29f4a4b” ]

Here is one approach to validate the configuration string and return the configurations in order:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import re

def process_config(config):

  # Validate configuration string
  if not re.fullmatch(r'\d{4}[a-zA-Z0-9]{10}\|\d{4}[a-zA-Z0-9]{10}\|\d{4}[a-zA-Z0-9]{10}', config):
    return ["Invalid configuration"]

  configs = []

  # Extract configurations    
  for c in config.split('|'):
    index = int(c[:4])
    value = c[4:]
    
    # Validate index and value
    if index == 0 or len(value) != 10 or not value.isalnum():
      return ["Invalid configuration"]

    configs.append(value)

  # Validate no duplicate indices or values
  if len(set(configs)) != len(configs):
    return ["Invalid configuration"]

  # Sort configurations by index order
  configs.sort(key=lambda c: int(config.split('|')[configs.index(c)][:4]))

  return configs

The key steps are:

  • Validate format using regex
  • Extract configurations
  • Validate index and value per configuration
  • Check no duplicates
  • Sort configurations by index

This validates the overall format, each configuration, and ordering to ensure a valid configuration string.