--- id: 69a890af247de743333bd4cc title: "Challenge 223: QR Decoder" challengeType: 29 dashedName: challenge-223 --- # --description-- Given a 6x6 matrix (array of strings), representing a QR code, return the string of binary data in the code. - The QR code may be given in any rotation of 90 degree increments. - A correctly oriented code has a 2x2 group of `1`'s (orientation markers) in the bottom-left, top-left, and top-right corners. - The three 2x2 orientation markers are not part of the binary data. - The binary data is read left-to-right, top-to-bottom (like a book) when the QR code is correctly oriented. - A code will always have exactly one valid orientation. For example, given: ```js [ "110011", "110011", "000000", "000000", "110000", "110001" ] ``` or given the same code with a different orientation: ```js [ "110011", "110011", "000000", "000000", "000011", "100011" ] ``` Return `"000000000000000000000001"`, all the binary data excluding the three 2x2 orientation markers. # --hints-- `decode_qr(["110011", "110011", "000000", "000000", "110000", "110001"])` should return `"000000000000000000000001"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_qr(["110011", "110011", "000000", "000000", "110000", "110001"]), "000000000000000000000001")`) }}) ``` `decode_qr(["100011", "000011", "000000", "000000", "110011", "110011"])` should return `"000000000000000000000001"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_qr(["100011", "000011", "000000", "000000", "110011", "110011"]), "000000000000000000000001")`) }}) ``` `decode_qr(["110011", "111111", "010000", "110000", "110011", "110100"])` should return `"001101000011000000110100"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_qr(["110011", "111111", "010000", "110000", "110011", "110100"]), "001101000011000000110100")`) }}) ``` `decode_qr(["011011", "101011", "101000", "100010", "110011", "111011"])` should return `"010001000100010101010110"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_qr(["011011", "101011", "101000", "100010", "110011", "111011"]), "010001000100010101010110")`) }}) ``` `decode_qr(["111100", "110001", "100011", "001101", "110011", "110011"])` should return `"010000100100100101001110"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(decode_qr(["111100", "110001", "100011", "001101", "110011", "110011"]), "010000100100100101001110")`) }}) ``` # --seed-- ## --seed-contents-- ```py def decode_qr(qr_code): return qr_code ``` # --solutions-- ```py def is_oriented(code): marker_indices = [ (0, 0), (0, 1), (0, 4), (0, 5), (1, 0), (1, 1), (1, 4), (1, 5), (4, 0), (4, 1), (5, 0), (5, 1) ] for row, col in marker_indices: if code[row][col] != '1': return False return True def rotate(code): new_code = ['', '', '', '', '', ''] for i in range(6): for j in range(5, -1, -1): new_code[i] += code[j][i] return new_code def get_data(code): data = '' data += code[0][2:4] data += code[1][2:4] data += code[2] data += code[3] data += code[4][2:] data += code[5][2:] return data def decode_qr(qr_code): code = qr_code[:] while not is_oriented(code): code = rotate(code) return get_data(code) ```