| <!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": "007 Split Words", "hasInstructions": true, "hasTests": true, "hasSolutions": true, "instructions": "<ol><li><p>Implement console program which meet the following requirements:</p><ol><li><p>Program starts and asks user to input any text</p></li><li><p>Program prints array of words entered by user without any spaces or punctuation marks</p></li></ol></li></ol>", "tests": [{"file_name": "Evaluate.java", "content": "import org.junit.Test;\nimport org.junit.Assert;\nimport com.udemy.ucp.*;\n\nimport static org.junit.Assert.assertEquals;\n\nimport java.io.ByteArrayInputStream;\nimport java.io.ByteArrayOutputStream;\nimport java.io.InputStream;\nimport java.io.PrintStream;\n\nimport org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\n\npublic class Evaluate {\n\n\tprivate static final String RESULT_SEPARATOR = \"You entered these words: \";\n\t\n\tprivate final ByteArrayOutputStream out = new ByteArrayOutputStream();\n\tprivate final PrintStream originalOut = System.out;\n\n\t@Before\n\tpublic void setStreams() {\n\t System.setOut(new PrintStream(out));\n\t}\n\n\t@After\n\tpublic void restoreInitialStreams() {\n\t System.setOut(originalOut);\n\t}\n\t\n\t@Test\n\tpublic void shouldSplitByAllPunctionaCharacters() {\n\t String input = \"Text with random punctuation marks: Java\\t is a general-purpose \"\n\t \t\t+ \"programming! \\'language\\'?? \\\"that\\\" ;is (class-based) and, \"\n\t \t\t+ \"object-oriented.\";\n\t InputStream in = new ByteArrayInputStream(input.getBytes());\n\t System.setIn(in);\n\n\t SplitWords.main(new String[] {});\n\t \n\t String[] result = out.toString().trim().split(RESULT_SEPARATOR);\n\t System.out.println(result[1]);\n\t\tassertEquals(\"[Text, with, random, punctuation, marks, \"\n\t\t\t\t+ \"Java, is, a, general, purpose, programming, \"\n\t\t\t\t+ \"language, that, is, class, based, and, object, \"\n\t\t\t\t+ \"oriented]\",result[1]);\n\t}\n\t\n}\n"}], "solutions": [{"file_name": "SplitWords.java", "content": "import java.util.Arrays;\r\nimport java.util.Scanner;\r\n\r\npublic class SplitWords {\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tSystem.out.print(\"Please, enter any text: \");\r\n\t\tString userInput = sc.nextLine();\r\n\t\tSystem.out.print(\"You entered these words: \");\r\n\t\tSystem.out.println(Arrays.toString(userInput.split(\"[\\\\p{P}\\\\s]+\"))); // POSIX character classes\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() { |
| |
| 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>"; |
| } |
| |
| |
| 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"); |
| } |
| |
| |
| 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> |
|
|