Cody

# Problem 34. Binary numbers

Solution 1779660

Submitted on 9 Apr 2019 by Yingao Zhang
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
n = 2; A = binary_numbers(n); assert(isequal(class(A), 'double'))

numdecmax = 3 B = "00" B = 1×2 string array "00" "01" B = 1×3 string array "00" "01" "10" B = 1×4 string array "00" "01" "10" "11" charls = NaN 0 0 NaN charls = 0 0 A = 0 0 charls = NaN 0 1 NaN charls = 0 1 A = 0 0 0 1 charls = NaN 1 0 NaN charls = 1 0 A = 0 0 0 1 1 0 charls = NaN 1 1 NaN charls = 1 1 A = 0 0 0 1 1 0 1 1

2   Pass
n = 3; A = binary_numbers(n); assert(all(A(:) == 0 | A(:) == 1))

numdecmax = 7 B = "000" B = 1×2 string array "000" "001" B = 1×3 string array "000" "001" "010" B = 1×4 string array "000" "001" "010" "011" B = 1×5 string array "000" "001" "010" "011" "100" B = 1×6 string array "000" "001" "010" "011" "100" "101" B = 1×7 string array "000" "001" "010" "011" "100" "101" "110" B = 1×8 string array "000" "001" "010" "011" "100" "101" "110" "111" charls = NaN 0 0 0 NaN charls = 0 0 0 A = 0 0 0 charls = NaN 0 0 1 NaN charls = 0 0 1 A = 0 0 0 0 0 1 charls = NaN 0 1 0 NaN charls = 0 1 0 A = 0 0 0 0 0 1 0 1 0 charls = NaN 0 1 1 NaN charls = 0 1 1 A = 0 0 0 0 0 1 0 1 0 0 1 1 charls = NaN 1 0 0 NaN charls = 1 0 0 A = 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 charls = NaN 1 0 1 NaN charls = 1 0 1 A = 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 charls = NaN 1 1 0 NaN charls = 1 1 0 A = 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 charls = NaN 1 1 1 NaN charls = 1 1 1 A = 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

3   Pass
n = 5; A = binary_numbers(n); assert(isequal(size(A),[32 5]))

numdecmax = 31 B = "00000" B = 1×2 string array "00000" "00001" B = 1×3 string array "00000" "00001" "00010" B = 1×4 string array "00000" "00001" "00010" "00011" B = 1×5 string array "00000" "00001" "00010" "00011" "00100" B = 1×6 string array "00000" "00001" "00010" "00011" "00100" "00101" B = 1×7 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" B = 1×8 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" B = 1×9 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" B = 1×10 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" B = 1×11 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" B = 1×12 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" B = 1×13 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" B = 1×14 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" B = 1×15 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" B = 1×16 string array "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" B = 1×17 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Column 17 "10000" B = 1×18 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 18 "10000" "10001" B = 1×19 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 19 "10000" "10001" "10010" B = 1×20 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 20 "10000" "10001" "10010" "10011" B = 1×21 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 21 "10000" "10001" "10010" "10011" "10100" B = 1×22 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 22 "10000" "10001" "10010" "10011" "10100" "10101" B = 1×23 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 23 "10000" "10001" "10010" "10011" "10100" "10101" "10110" B = 1×24 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 24 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" B = 1×25 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 25 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" B = 1×26 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 26 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" B = 1×27 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 27 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" "11010" B = 1×28 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 28 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" "11010" "11011" B = 1×29 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 29 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" "11010" "11011" "11100" B = 1×30 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 30 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" "11010" "11011" "11100" "11101" B = 1×31 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 31 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" "11010" "11011" "11100" "11101" "11110" B = 1×32 string array Columns 1 through 16 "00000" "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" "01010" "01011" "01100" "01101" "01110" "01111" Columns 17 through 32 "10000" "10001" "10010" "10011" "10100" "10101" "10110" "10111" "11000" "11001" "11010" "11011" "11100" "11101" "11110" "11111" charls = NaN 0 0 0 0 0 NaN charls = 0 0 0 0 0 A = 0 0 0 0 0 charls = NaN 0 0 0 0 1 NaN charls = 0 0 0 0 1 A = 0 0 0 0 0 0 0 0 0 1 charls = NaN 0 0 0 1 0 NaN charls = 0 0 0 1 0 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 charls = NaN 0 0 0 1 1 NaN charls = 0 0 0 1 1 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 charls = NaN 0 0 1 0 0 NaN charls = 0 0 1 0 0 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 charls = NaN 0 0 1 0 1 NaN charls = 0 0 1 0 1 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 charls = NaN 0 0 1 1 0 NaN charls = 0 0 1 1 0 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 1 0 charls = NaN 0 0 1 1 1 NaN charls = 0 0 1 1 1 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 1 charls = NaN 0 1 0 0 0 NaN charls = 0 1 0 0 0 A = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 charls = NaN 0 1 0 ...

4   Pass
n = 10; A = binary_numbers(n); assert(isequal(size(unique(A,'rows'),1),1024))

numdecmax = 1023 B = "0000000000" B = 1×2 string array "0000000000" "0000000001" B = 1×3 string array "0000000000" "0000000001" "0000000010" B = 1×4 string array "0000000000" "0000000001" "0000000010" "0000000011" B = 1×5 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" B = 1×6 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" B = 1×7 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" B = 1×8 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" B = 1×9 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" B = 1×10 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" B = 1×11 string array "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" B = 1×12 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Column 12 "0000001011" B = 1×13 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 13 "0000001011" "0000001100" B = 1×14 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 14 "0000001011" "0000001100" "0000001101" B = 1×15 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 15 "0000001011" "0000001100" "0000001101" "0000001110" B = 1×16 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 16 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" B = 1×17 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 17 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" B = 1×18 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 18 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" B = 1×19 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 19 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" B = 1×20 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 20 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" B = 1×21 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 21 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" B = 1×22 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" B = 1×23 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Column 23 "0000010110" B = 1×24 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 24 "0000010110" "0000010111" B = 1×25 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 25 "0000010110" "0000010111" "0000011000" B = 1×26 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 26 "0000010110" "0000010111" "0000011000" "0000011001" B = 1×27 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 27 "0000010110" "0000010111" "0000011000" "0000011001" "0000011010" B = 1×28 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 28 "0000010110" "0000010111" "0000011000" "0000011001" "0000011010" "0000011011" B = 1×29 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 29 "0000010110" "0000010111" "0000011000" "0000011001" "0000011010" "0000011011" "0000011100" B = 1×30 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 30 "0000010110" "0000010111" "0000011000" "0000011001" "0000011010" "0000011011" "0000011100" "0000011101" B = 1×31 string array Columns 1 through 11 "0000000000" "0000000001" "0000000010" "0000000011" "0000000100" "0000000101" "0000000110" "0000000111" "0000001000" "0000001001" "0000001010" Columns 12 through 22 "0000001011" "0000001100" "0000001101" "0000001110" "0000001111" "0000010000" "0000010001" "0000010010" "0000010011" "0000010100" "0000010101" Columns 23 through 31 "0000010110" "0000010111" "000001...

5   Pass
n = 1; A = binary_numbers(n); assert(isequal(A,[0;1]) || isequal(A,[1;0]))

numdecmax = 1 B = "0" B = 1×2 string array "0" "1" charls = NaN 0 NaN charls = 0 A = 0 charls = NaN 1 NaN charls = 1 A = 0 1