build-a-qr-code-encoder / lesson-20.md
Lesson 20 · Blocks and the codeword sequence

The full codeword sequence

Data codewords and their error-correction codewords finally come together into the single sequence the symbol will carry. Today you join the two halves you built in separate chapters for the HELLO WORLD block.

The goal

Produce the full codeword sequence: data codewords followed by error-correction codewords.

Start here - the target
TO DO
Scenario: Data and error correction form one sequence
Giventhe 13 HELLO WORLD data codewords and the level-Q count of 13 error-correction codewords
WhenrsEncode computes the EC codewords and they are appended after the data
Thenthe sequence is 26 codewords long: the 13 data codewords then [168, 72, 22, 82, 217, 54, 156, 0, 46, 15, 180, 122, 16]
Andthe full sequence is [32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 168, 72, 22, 82, 217, 54, 156, 0, 46, 15, 180, 122, 16]
Background

This is where the two big halves of the project meet. Chapter 3 turned text into data codewords; chapter 2 turned data codewords into error-correction codewords. The symbol carries both, in a defined order: for a single-block symbol like Version 1, it is simply all the data codewords followed by all the error-correction codewords.

Joining HELLO WORLD’s 13 data codewords with its 13 error-correction codewords gives a 26-codeword sequence - exactly the Version 1 capacity from the table. This sequence is what gets laid into the grid as bits, so getting its order right is what makes the finished symbol scannable. For Version 1 the order is trivial because there is one block, but the next two lessons build the general splitting and interleaving so the same code would place multi-block symbols correctly too.

Make it work
// Join the two halves the earlier chapters produced.
data := dataCodewords(text, "Q") // chapter 3
ec := rsEncode(data, 13) // chapter 2
seq := append(append([]byte(nil), data...), ec...)
CheckpointDONE
The complete codeword sequence for a symbol is assembled. Commit and stop here.