At My Fingertips
Rapid Playground
Look carefully at this image: you've probably seen similar ones many times.
It is legitimate to wonder whether there is some relationship between the numbers and the bars. In reality, they are simply two different ways to represent the same data.
There are numerous global standards to uniquely identify products on the market. One of these standards is GTIN.
There are several variants, among which GTIN-12 is among the most popular ones. It uniquely identifies each product with a 12-digit number (which in turn has a certain structure, with some digits identifying the manufacturer and others the specific product, but we won't go into details here).
The barcode shown above actually contains a GTIN-12 number twice: in the lower part the digits are written so that they are easily readable by a human ("human-readable"), while in the upper part they are coded with an alternating sequence of bars and spaces so as to be easily readable by a machine ("machine-readable").
The bar coding format shown above uses the UPC standard (Universal Product Code), in its UPC-A variant, which is capable of representing a 12-digit number.
This format simply defines a structure composed of 95 modules. A module is each of the vertical stripes, which are separated by a red line in the following figure, filled with black (what we colloquially call the "bar") or white (the "space").
We can bundle the modules in the following groups:
Before we get into coding the individual digits, try creating the "start" group by simply placing three rectangles beside each other:
For simplicity, let's start by encoding the first six digits (to be clear, those on the left, between the initial group and the central one).
Each digit is represented using 7 modules.
The digit 0, for example, is represented with the sequence space-space-space-bar-bar-space-bar.
For convenience, we can identify the space with the boolean value False
and the bar with the value True
.
Following the standard, we can create a list where the element in position i
indicates the encoding of the digit i
,
represented with a list of 7 boolean values.
Implement a function that creates the graphic corresponding to the given digit,
appropriately placing 7 modules one side by side
(remember that you can call the module
function defined above).
Don't worry about handling the case where left
is False
for now:
we will deal with the right side shortly.
The last six digits, to the right of the central part, are encoded by reversing the roles of bar and space: the digit 0, for example, is represented with the sequence bar-bar-bar-space-space-bar-space (exactly the opposite of the encoding for the left side).
Extend the implementation of the function you just defined to also handle the case where left
is False
.
We can now create the entire barcode, placing the groups of modules one next to the other as described in the bulleted list above.
You can verify that your barcode "really works" by scanning it with your smartphone using an application that reads barcodes (you can find free ones on the various stores).
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Barcode
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)