fork download
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.Set;
  7. import java.util.Iterator;
  8.  
  9. import static java.lang.System.*;
  10.  
  11. class Ideone {
  12. // userResults variables are where we write output we want the user to see
  13. // it functions just like System.out
  14.  
  15. // Readable -- via toString() -- version of userResults
  16. private OutputStream userResultsByteArray = new ByteArrayOutputStream();
  17. // Writable -- via println("") -- version of userResults
  18. private PrintStream userResults = new PrintStream(userResultsByteArray);
  19. // numberOfTests is incremented whenever a test case is evaluated
  20. private int numberOfTests = 0;
  21. // numberOfTestsPassed is incremented whenever a test case is evaluated successfully
  22. private int numberOfTestsPassed = 0;
  23. //(Integer) ((numberOfTestsPassed / numberOfTests)*100) == percent correct for student work
  24.  
  25. // Class autorun entry point
  26. public static void main(String[] args) throws Exception, IOException {
  27. new Ideone().test();
  28. }
  29.  
  30. // Put tests for specific template here
  31. private void test() throws Exception, IOException {
  32.  
  33. List<String> input;
  34. List<String> output;
  35.  
  36. // **** Add/edit test cases here ****
  37. input = Arrays.asList("34","\n","45","\n","55","\n","57","\n","79","\n","67","\n","68","\n","69","\n","72","\n","77","\n","88","\n","99","\n","101","\n","2345","\n","-10","\n","200","\n","200","\n","200","\n","200","\n","200","\n","-200","\n");
  38. output = Arrays.asList("Enter the values for the first array, up to 10000 values, enter a negative number to quit","","First Array", "34 45 55 57 79 67 68 69 72 77 88 99 101 2345", "Second Array", "200 200 200 200 200", "ERROR: Array not in correct order");
  39. assertRegex("Test:error", input, output);
  40.  
  41. input = Arrays.asList("12","\n","23","\n","34","\n","45","\n","56","\n","67","\n","78","\n","89","\n","90","\n","-100","21","\n","32","\n","43","\n","54","\n","65","\n","76","\n","87","\n","98","\n","100","\n","-1","\n");
  42. output = Arrays.asList("Enter the values for the first array, up to 10000 values, enter a negative number to quit","","First Array", "12 23 34 45 56 67 78 89 90", "Second Array", "21 32 43 54 65 76 87 98 100", "12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 90 98 100");
  43. assertRegex("Test:ok", input, output);
  44.  
  45. generateScoreResults();
  46.  
  47. }
  48.  
  49. // Scoring Utilities
  50. private void generateScoreResults(){
  51. System.out.println("Your code has been evaluated against a set of test data.");
  52. System.out.format("You had %d out of %d tests pass correctly.\r\n",
  53. numberOfTestsPassed, numberOfTests);
  54. System.out.format("Your score is %d%%.\r\n", calculateScore());
  55. // Share any messages from assertions about pass/fail on specific tests
  56. System.out.print(userResultsByteArray.toString());
  57. System.out.format("Secrets: {\"score\": %d}\r\n", calculateScore());
  58.  
  59. }
  60.  
  61. private int calculateScore(){
  62. double passed = (double) numberOfTestsPassed;
  63. double taken = (double) numberOfTests;
  64. return (int) (( passed / taken )*100);
  65. }
  66.  
  67. // Assertion/Testing Utilities
  68.  
  69. // assertRegex will run the student's code, providing "inputs" as STDIN
  70. // It will regex match the output generated by the student's code
  71. // against the values provided in expectedList
  72. private void assertRegex(String testName, List<String> inputs, List<String> expectedList) throws Exception, IOException {
  73. // Preserve StdOut
  74. PrintStream origOut = System.out;
  75. // redirect StdOut
  76. ByteArrayOutputStream actuals = setStdOut();
  77. // Inject test data into StdIn
  78. setStdIn(inputs);
  79. // Run student code w/inputs
  80. Main.main(new String[0]);
  81. // Eval expected results against stub System.out (byteArray)
  82. Scanner scanner = new Scanner(actuals.toString());
  83. // Loop through actual results, and look for matches with expected results
  84. // Remove expected value once it is found
  85. userResults.println(testName);
  86.  
  87. int numberOfTestCases = 1;
  88. try {
  89. for (int i = 0; i < expectedList.size(); i++){
  90. numberOfTests++;
  91. Boolean matched = false;
  92. String expected = expectedList.get(i);
  93. while (scanner.hasNextLine()){
  94. String actual = scanner.nextLine();
  95. if (actual.matches(".*"+expected+".*")) {
  96. numberOfTestsPassed++;
  97. matched = true;
  98. userResults.println(" Case #"+numberOfTestCases+" passed");
  99. // we replace the matched item with a regex /.\A/ which matches nothing
  100. // this is to prevent this expectation from matching another actual line of output
  101. expectedList.set(i, ".\\A");
  102. // break once we find a match for this test case, we don't want to match any more
  103. break;
  104. } // if (actual..
  105. } // while (scanner..
  106. if (!matched){
  107. userResults.println(" Case #"+numberOfTestCases+" failed");
  108. }
  109. // reset scanner for next operation
  110. scanner = new Scanner(actuals.toString());
  111. numberOfTestCases++;
  112. } // for (int i=...
  113. } finally {
  114. //restore StdOut
  115. System.setOut(origOut);
  116. }
  117. }
  118.  
  119. // assertNotMatch will run the student's code, providing "inputs" as STDIN
  120. // It will make sure that NONE of the output values provided in expectedList are found in ANY of the actual output
  121. private void assertNotMatch(String testName, List<String> inputs, List<String> expectedList) throws Exception, IOException {
  122. // Print test name to user
  123. userResults.println(testName);
  124. // Preserve StdOut
  125. PrintStream origOut = System.out;
  126. // redirect StdOut
  127. ByteArrayOutputStream actuals = setStdOut();
  128. // Inject test data into StdIn
  129. setStdIn(inputs);
  130. // Run student code w/inputs
  131. Main.main(new String[0]);
  132. // Load test output into scanner
  133. Scanner scanner = new Scanner(actuals.toString());
  134.  
  135. // Loop through test output (actuals), and look for matches with expected results
  136. // Fail test if a match is found
  137. int numberOfTestCases = 1;
  138. try {
  139. for (int i = 0; i < expectedList.size(); i++){
  140. numberOfTests++;
  141. Boolean passed = true;
  142. String expected = expectedList.get(i);
  143. while (scanner.hasNextLine()){
  144. String actual = scanner.nextLine();
  145. if (actual.matches(".*"+expected+".*")) {
  146. userResults.println(" Case #"+numberOfTestCases+" failed");
  147. passed = false;
  148. // break b/c this test case has failed and no further lines need to be examined
  149. break;
  150. } // if (actual..
  151. } // while (scanner..
  152. if (passed){
  153. numberOfTestsPassed++;
  154. userResults.println(" Case #"+numberOfTestCases+" passed");
  155. }
  156. // reset scanner for next operation
  157. scanner = new Scanner(actuals.toString());
  158. numberOfTestCases++;
  159. } // for (int i=...
  160. } finally {
  161. //restore StdOut
  162. System.setOut(origOut);
  163. }
  164. }
  165.  
  166.  
  167. //Standard In/Out Utilities
  168.  
  169. // Sets StdOut to a new ByteStream
  170. // Returns the ByteStream so whatever is written
  171. // can be read back out later via this object
  172. private ByteArrayOutputStream setStdOut(){
  173. ByteArrayOutputStream newStdOutByteArray = new ByteArrayOutputStream();
  174. PrintStream newStdOut = new PrintStream(newStdOutByteArray);
  175. System.setOut(newStdOut);
  176. return newStdOutByteArray;
  177. }
  178.  
  179. // set System.in/STDIN to string: newIn
  180. // Note: be sure to preserve System.in before calling, if you need it later
  181. // You can preserve System.in simply with InputStream oldIn = System.in;
  182. private void setStdIn(List<String> listIn){
  183. String strIn = joinCR(listIn);
  184. ByteArrayInputStream stubIn = new ByteArrayInputStream(strIn.getBytes());
  185. System.setIn(stubIn);
  186. }
  187.  
  188. // Basic Utilities
  189. //joins string list together w/carriage returns
  190. private String joinCR(List<String> list){
  191. return join(list, '\n');
  192. }
  193.  
  194. //join a list into a delimted string
  195. private String join(List<String> list, char delimiter){
  196. StringBuilder sb = new StringBuilder();
  197. for(String s: list) {
  198. sb.append(s).append(delimiter);
  199. }
  200. sb.deleteCharAt(sb.length()-1);
  201. return sb.toString();
  202. }
  203.  
  204. }//Ideone class
  205.  
  206. // Following line needs to be uncommented and system swaps in student code
  207. // in its place when run within GCB by code-runner system
  208.  
  209. //$$YIELD$$
  210.  
  211. class Main {
  212. /*
  213.   While Loops
  214.   Tracing Code and Counting the Number of Iterations
  215.   More Loops
  216.   Technique - flag variables
  217.   Strings as class types (vs primitive)
  218.   */
  219. //WildCard
  220. //assume only one * per string
  221.  
  222. public static void main (String str[]) throws IOException {
  223.  
  224. Scanner scan = new Scanner (System.in);
  225.  
  226. //length will be less than 10,000
  227. //count how many elements entered into each array
  228. int a1 = 0;
  229. int a2 = 0;
  230. int ar1[] = new int [10000];
  231. int ar2[] = new int [10000];
  232.  
  233. for (int i=0; i< ar1.length; i++)
  234. {
  235. ar1[i] = Integer.MAX_VALUE;
  236. ar2[i] = Integer.MAX_VALUE;
  237. }
  238.  
  239. int val =1;
  240. System.out.println("\n\nEnter the values for the first array, up to 10000 values, enter a negative number to quit");
  241. while (val >= 0)
  242. {
  243. val = scan.nextInt();
  244. if (val >= 0)
  245. {
  246. ar1[a1] = val;
  247. a1++;
  248. }
  249. }
  250.  
  251.  
  252.  
  253. //enter 2nd array
  254. val =1;
  255. System.out.println("\n\nEnter the values for the second array, up to 10000 values, enter a negative number to quit");
  256. while (val >= 0)
  257. {
  258. val = scan.nextInt();
  259. if (val >= 0)
  260. {
  261. ar2[a2] = val;
  262. a2++;
  263. }
  264. }
  265.  
  266. System.out.println("\n\nFirst Array: ");
  267. for (int i =0; i < a1; i++)
  268. System.out.print(ar1[i] + " ");
  269.  
  270. System.out.println("\n\nSecond Array: ");
  271. for (int i =0; i < a2; i++)
  272. System.out.print(ar2[i] + " ");
  273.  
  274.  
  275.  
  276.  
  277. //check if in order
  278. boolean inOrder = true;
  279.  
  280. for (int i = 1; i < a1; i++)
  281. {
  282. if (ar1[i-1] > ar1[i])
  283. inOrder = false;
  284. }
  285. for (int i = 1; i < a2; i++)
  286. {
  287. if (ar2[i-1] > ar2[i])
  288. inOrder = false;
  289. }
  290. // System.out.println("\n\nIn order?? " + inOrder);
  291.  
  292.  
  293. if (!inOrder)
  294. {
  295. System.out.println("\n\nERROR: Array not in correct order");
  296. }
  297. else
  298. {
  299.  
  300. //merge and output
  301. int merge [] = new int [a1 + a2];
  302. //System.out.println("\n\nLength of new array: " + merge.length);
  303. a1 = 0;
  304. a2 = 0;
  305. for (int i = 0; i < merge.length; i++)
  306. {
  307. if (ar1[a1] <= ar2[a2])
  308. {
  309. merge[i] = ar1[a1];
  310. a1++;
  311. }
  312. else
  313. {
  314. merge[i] = ar2[a2];
  315. a2++;
  316. }
  317. }
  318. System.out.println("\n\nMerged Array: ");
  319. for (int i =0; i < merge.length; i++)
  320. System.out.print(merge[i] + " ");
  321. }
  322. }
  323.  
  324. }
Success #stdin #stdout 0.24s 61388KB
stdin
Standard input is empty
stdout
Your code has been evaluated against a set of test data.
You had 14 out of 14 tests pass correctly.
Your score is 100%.
Test:error
  Case #1 passed
  Case #2 passed
  Case #3 passed
  Case #4 passed
  Case #5 passed
  Case #6 passed
  Case #7 passed
Test:ok
  Case #1 passed
  Case #2 passed
  Case #3 passed
  Case #4 passed
  Case #5 passed
  Case #6 passed
  Case #7 passed
Secrets: {"score": 100}