File size: 11,529 Bytes
33852e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Coding Assignment</title>

        <style>
            body {
                font-family: sf pro text, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial,
                    sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
                font-weight: 400;
                line-height: 22.4px;
                font-size: 16px;
            }
            p,
            ul,
            ol {
                font-size: 16px;
                font-weight: 400;
            }
            h1,
            h2,
            h3,
            h4,
            h5,
            h6 {
                font-weight: bold;
            }
            ul {
                list-style: none;
                margin: 0;
                padding: 0;
                max-width: none;
            }
            .code-snippet {
                background-color: #fff;
                border: 1px solid #d1d7dc;
                color: #b4690e;
                font-size: 90%;
                padding: 0.2rem 0.4rem;
            }
            .code-block {
                background-color: #fff;
                color: #b4690e;
                font-size: 90%;
            }
            .black-block {
                color: #000000;
            }
            .italic-text {
                font-style: italic;
            }
        </style>
    </head>

    <body onload="main()">
        <h1 id="coding-title"></h1>
        <div>
            <h2>Instructions</h2>
            <div id="coding-instructions"></div>
        </div>
        <div>
            <h2>Test(s)</h2>
            <div id="coding-tests"></div>
        </div>
        <div>
            <h2>Solution(s)</h2>
            <div id="coding-solutions"></div>
        </div>

        <script>
            const quizData = {"title": "009 Rotate Matrix", "hasInstructions": true, "hasTests": true, "hasSolutions": true, "instructions": "<ol><li><p>Implement console program which will meet the following requirements:</p><ol><li><p>Program starts and asks user to specify matrix size. For example if user entered \u20185\u2019 program will generate next matrix:<br><br>0.0 0.1 0.2 0.3 0.4<br>1.0 1.1 1.2 1.3 1.4<br>2.0 2.1 2.2 2.3 2.4<br>3.0 3.1 3.2 3.3 3.4<br>4.0 4.1 4.2 4.3 4.4<br></p></li><li><p>Program asks user this question with options: <br>How much do you want to rotate matrix? <br>- Press 1 to rotate matrix to 90 degrees<br>- Press 2 to rotate matrix to 180 degrees<br>- Press 3 to rotate matrix to 270 degrees</p></li><li><p>When user chosen rotation mode programs prints initial matrix and rotated one.</p></li><li><p>Rotation for 90 degrees looks like this:<br>4.0 3.0 2.0 1.0 0.0<br>4.1 3.1 2.1 1.1 0.1<br>4.2 3.2 2.2 1.2 0.2<br>4.3 3.3 2.3 1.3 0.3<br>4.4 3.4 2.4 1.4 0.4<br></p></li><li><p>Rotation for 180 degrees looks like this:<br>4.4 4.3 4.2 4.1 4.0<br>3.4 3.3 3.2 3.1 3.0<br>2.4 2.3 2.2 2.1 2.0<br>1.4 1.3 1.2 1.1 1.0<br>0.4 0.3 0.2 0.1 0.0<br></p></li><li><p>Rotation for 270 degrees looks like this:<br>0.4 1.4 2.4 3.4 4.4<br>0.3 1.3 2.3 3.3 4.3<br>0.2 1.2 2.2 3.2 4.2<br>0.1 1.1 2.1 3.1 4.1<br>0.0 1.0 2.0 3.0 4.0<br><br></p></li></ol></li></ol><p>You have to implement next methods:</p><p><strong><em>public static void rotate90(double[][] matrix) {</em></strong></p><p><strong><em>&nbsp; &nbsp; &nbsp;&lt;write your code here&gt;</em></strong></p><p><strong><em>}</em></strong></p><p><br></p><p><strong><em>public static void rotate180(double[][] matrix) {</em></strong></p><p><strong><em>&nbsp; &nbsp; &nbsp; &lt;write your code here&gt;</em></strong></p><p><strong><em>}</em></strong></p><p><br></p><p><strong><em>public static void rotate270(double[][] matrix) {</em></strong></p><p><strong><em>&nbsp; &nbsp; &nbsp; &nbsp;&lt;write your code here&gt;</em></strong></p><p><strong><em>}</em></strong></p>", "tests": [{"file_name": "Evaluate.java", "content": "import org.junit.Test;\nimport org.junit.Assert;\nimport com.udemy.ucp.*;\n\nimport static org.junit.Assert.assertTrue;\n\nimport java.util.Arrays;\n\nimport org.junit.Test;\n\npublic class Evaluate {\n\t\n@Test\n\tpublic void shouldRotate90() {\n\t\tdouble[][] matrix = {\n\t\t\t\t{10.0, 10.1, 10.2},\n\t\t\t\t{32.0, 32.1, 32.2},\n\t\t\t\t{47.0, 47.1, 47.2},\n\t\t};\n\t\tMatrixRotation.rotate90(matrix);\n\t\t\n\t\tdouble[][] expectedMatrix = {\n\t\t\t\t{47.0, 32.0, 10.0},\n\t\t\t\t{47.1, 32.1, 10.1},\n\t\t\t\t{47.2, 32.2, 10.2},\n\t\t};\n\t\tassertTrue(Arrays.deepEquals(expectedMatrix, matrix));\n\t}\n\t\n\t@Test\n\tpublic void shouldRotate180() {\n\t\tdouble[][] matrix = {\n\t\t\t\t{10.0, 10.1, 10.2},\n\t\t\t\t{32.0, 32.1, 32.2},\n\t\t\t\t{47.0, 47.1, 47.2},\n\t\t};\n\t\tMatrixRotation.rotate180(matrix);\n\t\t\n\t\tdouble[][] expectedMatrix = {\n\t\t\t\t{47.2, 47.1, 47.0},\n\t\t\t\t{32.2, 32.1, 32.0},\n\t\t\t\t{10.2, 10.1, 10.0},\n\t\t};\n\t\tassertTrue(Arrays.deepEquals(expectedMatrix, matrix));\n\t}\n\t\n\t@Test\n\tpublic void shouldRotate270() {\n\t\tdouble[][] matrix = {\n\t\t\t\t{10.0, 10.1, 10.2},\n\t\t\t\t{32.0, 32.1, 32.2},\n\t\t\t\t{47.0, 47.1, 47.2},\n\t\t};\n\t\tMatrixRotation.rotate270(matrix);\n\t\t\n\t\tdouble[][] expectedMatrix = {\n\t\t\t\t{10.2, 32.2, 47.2},\n\t\t\t\t{10.1, 32.1, 47.1},\n\t\t\t\t{10.0, 32.0, 47.0},\n\t\t};\n\t\tassertTrue(Arrays.deepEquals(expectedMatrix, matrix));\n\t}\n\n}\n"}], "solutions": [{"file_name": "MatrixRotation.java", "content": "import java.util.Scanner;\r\n\r\npublic class MatrixRotation {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner in = new Scanner(System.in);\r\n\r\n\t\tSystem.out.print(\"Please, enter matrix size: \");\r\n\t\tint size = in.nextInt();\r\n\t\tdouble[][] matrix = generateMatrix(size);\r\n\r\n\t\tSystem.out.println(\"How you want to rotate matrix:\" + System.lineSeparator() +\r\n\t\t\t\t\"\\t1 - 90\" + System.lineSeparator() +\r\n\t\t\t\t\"\\t2 - 180\" + System.lineSeparator() +\r\n\t\t\t\t\"\\t3 - 270\");\r\n\t\tint mode = in.nextInt();\r\n\r\n\t\tSystem.out.println(System.lineSeparator() + \"Base matrix:\" + System.lineSeparator());\r\n\t\tprintMatrixToConsole(matrix);\r\n\t\tSystem.out.println();\r\n\r\n\t\tif (rotateMatrix(matrix, mode)) {\r\n\t\t\tprintMatrixToConsole(matrix);\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static double[][] generateMatrix(int size) {\r\n\t\tdouble[][] matrix = new double[size][size];\r\n\r\n\t\tfor (int i = 0; i < matrix.length; i++) {\r\n\t\t\tfor (int j = 0; j < matrix.length; j++) {\r\n\t\t\t\tmatrix[i][j] = Double.valueOf(Integer.toString(i) + \".\" \r\n\t\t\t\t\t\t+ Integer.toString(j));\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn matrix;\r\n\t}\r\n\r\n\tprivate static boolean rotateMatrix(double[][] matrix, int mode) {\r\n\t\tswitch (mode) {\r\n\t\t\tcase 1:\r\n\t\t\t\tSystem.out.println(\"90 degrees rotated:\" + System.lineSeparator());\r\n\t\t\t\trotate90(matrix);\r\n\t\t\t\tbreak;\r\n\t\t\tcase 2:\r\n\t\t\t\tSystem.out.println(\"180 degrees rotated:\" + System.lineSeparator());\r\n\t\t\t\trotate180(matrix);\r\n\t\t\t\tbreak;\r\n\t\t\tcase 3:\r\n\t\t\t\tSystem.out.println(\"270 degrees rotated:\" + System.lineSeparator());\r\n\t\t\t\trotate270(matrix);\r\n\t\t\t\tbreak;\r\n\t\t\tdefault:\r\n\t\t\t\tSystem.out.println(\"You selected non-existing mode!\");\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\treturn true;\r\n\t}\r\n\t\r\n\tprivate static void transposeMatrix(double[][] matrix) {\r\n\t\tdouble temp;\r\n\t\tfor (int i = 0; i < matrix.length; i++) {\r\n\t\t\tfor (int j = 0; j < i; j++) {\r\n\t\t\t\ttemp = matrix[i][j];\r\n\t\t\t\tmatrix[i][j] = matrix[j][i];\r\n\t\t\t\tmatrix[j][i] = temp;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static void verticalReflection(double[][] matrix) {\r\n\t\tdouble temp;\r\n\t\tfor (int i = 0; i < matrix.length; i++) {\r\n\t\t\tfor (int j = 0; j < matrix.length / 2; j++) {\r\n\t\t\t\ttemp = matrix[i][j];\r\n\t\t\t\tmatrix[i][j] = matrix[i][matrix.length - 1 - j];\r\n\t\t\t\tmatrix[i][matrix.length - 1 - j] = temp;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static void horizontalReflection(double[][] matrix) {\r\n\t\tdouble temp;\r\n\t\tfor (int i = 0; i < matrix.length / 2; i++) {\r\n\t\t\tfor (int j = 0; j < matrix.length; j++) {\r\n\t\t\t\ttemp = matrix[i][j];\r\n\t\t\t\tmatrix[i][j] = matrix[matrix.length - 1 - i][j];\r\n\t\t\t\tmatrix[matrix.length - 1 - i][j] = temp;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void rotate90(double[][] matrix) {\r\n\t\ttransposeMatrix(matrix);\r\n\t\tverticalReflection(matrix);\r\n\t}\r\n\r\n\tpublic static void rotate180(double[][] matrix) {\r\n\t\tverticalReflection(matrix);\r\n\t\thorizontalReflection(matrix);\r\n\t}\r\n\r\n\tpublic static void rotate270(double[][] matrix) {\r\n\t\ttransposeMatrix(matrix);\r\n\t\thorizontalReflection(matrix);\r\n\t}\r\n\r\n\tprivate static void printMatrixToConsole(double[][] matrix) {\r\n\t\tfor (int i = 0; i < matrix.length; i++) {\r\n\t\t\tfor (int j = 0; j < matrix.length; j++) {\r\n\t\t\t\tSystem.out.print(matrix[i][j] + \"\\t\");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n\t\r\n\r\n}\r\n"}]};

            function renderCodeList(rootElement, codeList, className, titlePrefix) {
                for (var i = 0; i < codeList.length; i++) {
                    var elem = codeList[i];
                    var jsElem = document.createElement("div");
                    jsElem.className = className;
                    var jsElemTitle = document.createElement("h3");
                    jsElemTitle.innerHTML = titlePrefix + " " + (i + 1);
                    var jsElemBody = document.createElement("code");
                    jsElemBody.className = "code-block black-block";
                    jsElemBody.innerHTML = "<pre>" + elem.content + "</pre>";
                    jsElem.appendChild(jsElemTitle);
                    jsElem.appendChild(jsElemBody);
                    rootElement.appendChild(jsElem);
                }
            }

            function main() {
                // display the assignment
                var codingTitle = document.getElementById("coding-title");
                codingTitle.innerHTML = quizData.title;

                var codingInstructions = document.getElementById("coding-instructions");
                if (quizData.hasInstructions) {
                    codingInstructions.innerHTML = quizData.instructions;
                } else {
                    codingInstructions.innerHTML = '<span class="italic-text">' + quizData.instructions + "</span>";
                }

                // display the test(s)
                var codingTests = document.getElementById("coding-tests");
                if (!quizData.hasTests) {
                    codingTests.innerHTML = '<span class="italic-text">' + quizData.tests + "</span>";
                } else {
                    renderCodeList(codingTests, quizData.tests, "coding-test", "Test");
                }

                // display the solution(s)
                var codingSolutions = document.getElementById("coding-solutions");
                if (!quizData.hasSolutions) {
                    codingSolutions.innerHTML = '<span class="italic-text">' + quizData.solutions + "</span>";
                } else {
                    renderCodeList(codingSolutions, quizData.solutions, "coding-solution", "Solution");
                }
            }
        </script>
    </body>
</html>