4 kyu

Create the QR-Code

25 of 124zLuki

Description:

Overview

Your task is to encode a QR code. You get a string, of at most seven characters and it contains at least one letter.
You should return a QR code as a 2 dimensional array, filled with numbers, 1 is for a black field and 0 for a white field. We are limiting ourself to version 1, always usebyte mode, mask 0, and Error Correction Lvl H.

The way to encode a QR code is explained below.

Input/output

  • Input: string of at most 7 characters.
  • Output: 2 dimensional array, according to the process described below.
const text = "Hi";
return [[ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 ],
        [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 ],
        [ 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ],
        [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ],
        [ 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1 ],
        [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1 ],
        [ 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 ],
        [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
        [ 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1 ],
        [ 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 ],
        [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 ],
        [ 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0 ],
        [ 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 ],
        [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0 ],
        [ 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1 ],
        [ 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1 ],
        [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1 ],
        [ 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ],
        [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1 ],
        [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0 ],
        [ 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 ]];
String text = "Hi";
return new int[][]{ {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1},
                    {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1},
                    {1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1},
                    {1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
                    {1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1},
                    {1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1},
                    {1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1},
                    {1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1},
                    {1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1},
                    {1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0},
                    {1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0},
                    {1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1},
                    {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1},
                    {1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1},
                    {1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0},
                    {1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
                    {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0},
                    {1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1}};
text = "Hi"
return = [[ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 ],
          [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 ],
          [ 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ],
          [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ],
          [ 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1 ],
          [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1 ],
          [ 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 ],
          [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ],
          [ 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1 ],
          [ 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 ],
          [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 ],
          [ 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0 ],
          [ 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 ],
          [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0 ],
          [ 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1 ],
          [ 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1 ],
          [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1 ],
          [ 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ],
          [ 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1 ],
          [ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0 ],
          [ 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 ]]
string text = "Hi";
return new int[][]{ new int[]{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1},
                    new int[]{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1},
                    new int[]{1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1},
                    new int[]{1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
                    new int[]{1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1},
                    new int[]{1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1},
                    new int[]{1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1},
                    new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                    new int[]{0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1},
                    new int[]{1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1},
                    new int[]{1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1},
                    new int[]{1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0},
                    new int[]{1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0},
                    new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0},
                    new int[]{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1},
                    new int[]{1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1},
                    new int[]{1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1},
                    new int[]{1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0},
                    new int[]{1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
                    new int[]{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0},
                    new int[]{1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1}};

Debugging

It can be quite frustrating to find a mistake in a 21x21 matrix. Therefore you can use the preloaded function htmlize(), which takes a 2 dimensional array and returns a html graphical view, that you can print to the console.

console.log(htmlize(arr));
System.out.println(Preloaded.htmlize(arr));
print(htmlize(arr))
Console.WriteLine(Preloaded.htmlize(arr));

Note: htmlize() will throw an error if you don't pass a 21x21 matrix or if there is an invalid character in your matrix (anything other than 0 and 1).

Encoding a QR-code

Here comes the explaination on how to encode a QR code. You can skip it if you already know how it works.
As an example, we are going to encode the string "Hi".

Data encoding

We have to build a binary sequence which represents the data of the QR code. First of all, we add 0100 to our bit sequence because that stands for byte mode.

  bits = "0100";

Now we have to convert the length of our string to a 8 bit binary string:
The length of Hi is 2 which is 00000010 as a 8 bit binary string.

Now we append that binary string to our bit sequence:

  bits = "010000000010"

Now we convert the ascii value of every char of our string to a 8 bit binary string and append it to our bit sequence:

H --> 72
72 --> 01001000

  bits = "01000000001001001000"

i --> 105
105 --> 01101001

  bits = "0100000000100100100001101001"

Now we have to append 0000 as a terminator to our bit sequence, but our sequence must not be longer than 72 bits!
Some examples:

  • code length: 50 --> add 0000
  • code length: 70 --> add 00
  • code length: 71 --> add 0
  • code length: 72 --> add nothing

In our example the bit sequence is 28 long, so we add 0000.

  bits = "01000000001001001000011010010000"

As long as the length of our sequence isn't 72, we alternately append 11101100 and 00010001:

  Append 11101100
  bits = "0100000000100100100001101001000011101100"
  
  Append 00010001
  bits = "010000000010010010000110100100001110110000010001"
  
  Append 11101100
  bits = "01000000001001001000011010010000111011000001000111101100"
  
  etc..

Now we've got our bit sequence!

  bits = "010000000010010010000110100100001110110000010001111011000001000111101100"

Error Correction

It often happens that a QR Code was not scanned nicely or it is no longer recognizable in some places; therefore we need error corection bits. QR codes use the Reed-Solomon algorithm to calculate those error correction bits. This algorithm is very complex; that's the reason why this kata is blue.

For version 1, and Error Correction Lvl H, we need 17 Error Correction numbers.

First of all we have to create a message polynomial, it is structured like this:

messagePolynomial=ax8+bx7+cx6+dx5+ex4+fx3+gx2+hx1+ix0.\text{messagePolynomial} = ax^8+bx^7+cx^6+dx^5+ex^4+fx^3+gx^2+hx^1+ix^0.

We have to multiply this by x to the power of the number of Error Correction numbers.
We need 17 so we multiply it by x**17 to get:

messagePolynomial=ax25+bx24+cx23+dx22+ex21+fx20+gx19+hx18+ix17.\text{messagePolynomial} = ax^{25}+bx^{24}+cx^{23}+dx^{22}+ex^{21}+fx^{20}+gx^{19}+hx^{18}+ix^{17}.

Now we split the bit sequence in groups of 8 and convert it to a decimal number:

 group1 = 01000000 --> 64
 group2 = 00100100 --> 36
 etc..

For a,b,c,d,e,f,g,h,i insert the decimal numbers.
We get:

messagePolynomial=64x25+36x24+134x23+144x22+236x21+17x20+236x19+17x18+236x17.\text{messagePolynomial} = 64x^{25}+36x^{24}+134x^{23}+144x^{22}+236x^{21}+17x^{20}+236x^{19}+17x^{18}+236x^{17}.

We also need a generator polynomial. If you want to know how to create a generator polynomial, read this.
In this kata we will use the following as generator polynomial because we are limiting ourselfs to vesion 1:

g=α0x25+α43x24+α139x23+α206x22+α78x21+α43x20+α239x19+α123x18+α206x17+α214x16+α147x15+α24x14+α99x13+α150x12+α39x11+α243x10+α163x9+α136x8.\text{g} = \alpha^{0}x^{25}+\alpha^{43}x^{24}+\alpha^{139}x^{23}+\alpha^{206}x^{22}+\alpha^{78}x^{21}+\alpha^{43}x^{20}+\alpha^{239}x^{19}+\alpha^{123}x^{18}+\alpha^{206}x^{17}+\alpha^{214}x^{16}+\alpha^{147}x^{15}+\alpha^{24}x^{14}+\alpha^{99}x^{13}+\alpha^{150}x^{12}+\alpha^{39}x^{11}+\alpha^{243}x^{10}+\alpha^{163}x^{9}+\alpha^{136}x^{8}.

Now we have to multiply the generator polynomial with the lead term of the message polynomial.
The lead term of the message polynomial is 64x**25. We need to convert the integer 64 to alpha notation by looking it up in this table.
This table is already preloaded as a const array alphaTable. Use the integer as index and it returns the alpha exponent. For example:

let alphaExponent = alphaTable[64]; // Returns 6
int alphaExponent = Preloaded.alphaTable[64] // Returns 6
alphaExponent = alphaTable[64] # Returns 6

According to the table we can say:

α6=64\alpha^{6} = 64

We multiply the generator polynomial by adding the alpha exponents together.
We get:

α6x25+α49x24+α145x23+α212x22+α84x21+α49x20+α245x19+α129x18+α212x17+α220x16+α153x15+α30x14+α105x13+α156x12+α45x11+α249x10+α169x9+α142x8.\alpha^{6}x^{25}+\alpha^{49}x^{24}+\alpha^{145}x^{23}+\alpha^{212}x^{22}+\alpha^{84}x^{21}+\alpha^{49}x^{20}+\alpha^{245}x^{19}+\alpha^{129}x^{18}+\alpha^{212}x^{17}+\alpha^{220}x^{16}+\alpha^{153}x^{15}+\alpha^{30}x^{14}+\alpha^{105}x^{13}+\alpha^{156}x^{12}+\alpha^{45}x^{11}+\alpha^{249}x^{10}+\alpha^{169}x^{9}+\alpha^{142}x^{8}.

IMPORTANT NOTE: it can happen that the exponent becomes bigger than 254. If that happens modulo the exponent with 255!

Now we can convert all alpha exponents back to integers. Again, we are using this table to do that and we get:

64x25+140x24+77x23+121x22+107x21+140x20+233x19+23x18+121x17+172x16+146x15+96x14+26x13+228x12+193x11+54x10+229x9+42x8.64x^{25}+140x^{24}+77x^{23}+121x^{22}+107x^{21}+140x^{20}+233x^{19}+23x^{18}+121x^{17}+172x^{16}+146x^{15}+96x^{14}+26x^{13}+228x^{12}+193x^{11}+54x^{10}+229x^{9}+42x^{8}.

Now XOR these integer coefficients with the coefficients of the message polynomial. We get:

0x25+168x24+203x23+233x22+135x21+157x20+5x19+6x18+149x17+172x16+146x15+96x14+26x13+228x12+193x11+54x10+229x9+42x8.0x^{25}+168x^{24}+203x^{23}+233x^{22}+135x^{21}+157x^{20}+5x^{19}+6x^{18}+149x^{17}+172x^{16}+146x^{15}+96x^{14}+26x^{13}+228x^{12}+193x^{11}+54x^{10}+229x^{9}+42x^{8}.

If we did everything correct, the lead term has a 0 as coefficient. We can discard the lead term to get the following smaller polynomial which is our new message polynomial:

168x24+203x23+233x22+135x21+157x20+5x19+6x18+149x17+172x16+146x15+96x14+26x13+228x12+193x11+54x10+229x9+42x8.168x^{24}+203x^{23}+233x^{22}+135x^{21}+157x^{20}+5x^{19}+6x^{18}+149x^{17}+172x^{16}+146x^{15}+96x^{14}+26x^{13}+228x^{12}+193x^{11}+54x^{10}+229x^{9}+42x^{8}.

Note: Very rarely it can happen that the new lead term is also 0, in that case you also have to discard the second term/new lead term. This will affect how many times you repeat the next step.

The generator polynomial is still (and always) the same:

α0x25+α43x24+α139x23+α206x22+α78x21+α43x20+α239x19+α123x18+α206x17+α214x16+α147x15+α24x14+α99x13+α150x12+α39x11+α243x10+α163x9+α136x8.\alpha^{0}x^{25}+\alpha^{43}x^{24}+\alpha^{139}x^{23}+\alpha^{206}x^{22}+\alpha^{78}x^{21}+\alpha^{43}x^{20}+\alpha^{239}x^{19}+\alpha^{123}x^{18}+\alpha^{206}x^{17}+\alpha^{214}x^{16}+\alpha^{147}x^{15}+\alpha^{24}x^{14}+\alpha^{99}x^{13}+\alpha^{150}x^{12}+\alpha^{39}x^{11}+\alpha^{243}x^{10}+\alpha^{163}x^{9}+\alpha^{136}x^{8}.

Now perform these steps 8 more times
If you are wondering why 8 times, the length of our initial message polynomial was 9, we did it already once, eight times to go.
Be careful with the case where you had to discard two lead terms. If it is still hard to undestand you can follow each step very precisely here. You can also input a custom message polynomial but always use 17 error blocks.

The coefficients will be our Error Correction numbers:
250 255 60 175 138 205 169 12 87 238 222 39 87 58 213 84 70.

Note: Like I told you before very rarely both lead terms can be 0 in the discard lead term step. In that case you didn't get 17 error correction numbers, but less. So always check if you got 17 numbers, if not you have to append 0 at the front of our error correction numbers.

We convert all of these numbers to 8 bit binary strings and append it to our bit sequence from the Data Encoding step. Our finished sequence is now:

  bits = "0100000000100100100001101001000011101100000100011110110000010001111011001111101011111111001111001010111110001010110011011010100100001100010101111110111011011110001001110101011100111010110101010101010001000110".

Matrix placement, Format and Version Information

Every QR code has three big positioning field to enable the scanner to detect the orientation. These fields are located in the upper two and the lower left corner. They always have the same size so they should be hardcoded. qrmatrixpkf2

Also, the connections between the position fields are always the same.

Now we have to add the information about version, mask, and error correction level. In this kata we always use version 1 , mask 0 and error correction lvl H. Therfore you can also hardcode this information; every Qr code should look like this, before we even start to place in our data: qrmatrixti2

Insert Data

Now we start to insert our bit sequence into the QR matrix. The way we go through the matrix is very important. We start in the lower right corner, then we go one to the left, then one to the top and one to the right, then left again and so on. It's easier to follow the red arrow on the following picture.
Important: We have to skip the positioning fields and the information about version, mask and EC lvl. Just skip everything which is in a blue box in this picture: dcode-image-v3

Here is one more picture, that shows us how to go through the matrix: 1920px-QR_Character_Placement.svg

Now that we know how to go through the matrix we can start to insert our data.
Now another important thing comes into play: the mask

Normally every 1 in our bit sequence represents a black square and every 0 represents a white square but if the condition (x+y)%2==0 is true, we have to invert the color. 1 becomes a white square and 0 becomes a black square.
x is the line counter of the 21x21 matrix.
y is the column counter of the 21x21 matrix.

Ok, let's start:

  • First bit of our sequence is 0

  • We start in the lower right corner, therefore x is 20 and y is 20 (matrix is 0-based)

  • (x+y)%2==0 is true, therefore instead of a white square we place a black square in the lower right corner

    black-Corner
  • Let's take the next bit, which is 1

  • Now x is still 20 but y is now 19, because we went one to the left

  • (x+y)%2==0 is false, so we we insert a black square

etc...

Finally we get the following QR-code, which is the representation of the string "Hi": finished-Qr-Code

Mathematics
Algorithms

More By Author:

Check out these other kata created by zLuki

Stats:

CreatedNov 6, 2020
PublishedNov 7, 2020
Warriors Trained1421
Total Skips214
Total Code Submissions1683
Total Times Completed124
JavaScript Completions25
Java Completions31
Python Completions56
C# Completions19
Total Stars206
% of votes with a positive feedback rating88% of 46
Total "Very Satisfied" Votes38
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes3
Total Rank Assessments4
Average Assessed Rank
4 kyu
Highest Assessed Rank
2 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • zLuki Avatar
  • B1ts Avatar
  • hobovsky Avatar
  • rge123 Avatar
Ad