Problem #1 3 [2, 3, 7] 1 [2, 3, 7] 17 7 first 2 lines are 2 points third line is 1 point formula for 3rd line: (2x'+6) 3 -1 per incorrect token on a given line -0 for quotes -0 for {} or () instead of [] -1 off the top if a non-quote formatting error (underscores okay for spaces) ------------------------------------------------------------------------------- Problem #2: Original List Final List -------------------------------------------------------------- {1} {1} {2, 5, 9} {2, 7, 9} {2, 3, 7, 2} {2, 5, 2, 2} {2, 4, 5, 4, 3} {2, 6, -1, 3, 3} {5, 1, 8, 4, 8, 4} {5, 6, 2, 6, 2, 4} 2 points for each answer -1 for each wrong number -1 for each missing number curly braces and commas are not required if sequence is clear any given answer can't go negative ------------------------------------------------------------------------------- Problem #3: d a d b c 1 a 1 d 1 c 1 d 2 d 2 d 2 d 2 1/2 point each (half points okay in final score) -0 if spaces are not obvious (e.g., "a1" versus "a 1") -1 off the top if any formatting error (quotes, labels, commas, etc) -1 if first row appears as last row -2 if values transposed boxes/lines/grid/arrows are all okay because they are clearly not output ------------------------------------------------------------------------------- Problem #4 Solution: public static void switchData(Scanner input) { String title = input.next(); System.out.print(title); while (input.hasNextInt()) { int n1 = input.nextInt(); if (input.hasNextInt()) { int n2 = input.nextInt(); System.out.print(" " + n2); } System.out.print(" " + n1); } System.out.println(); } 1 header (non-void return type ok if consistent with a return statement) 1 reads/prints title once (println okay, can't be inside their loop) 1 correct while loop (while hasNext or while hasNextInt) 1 reads two numbers inside loop for either even or odd case 3 correctly prints the two numbers 1 attempt (two print or println statements) 2 correct order (reversed) and correct structure to print each pair once in the even case 2 properly handles case with odd number of numbers 1 attempt 1 correct 1 attempt to print all elements (title, two numbers) and all formatting correct (all prints, spaces between numbers, println after loop) ------------------------------------------------------------------------------- Problem #5 Solutions: public static int analyzeParagraphs(Scanner input) { int max = 0; while (input.hasNextLine()) { String line = input.nextLine(); int count = 0; while (!line.equals("<p>")) { count++; line = input.nextLine(); } System.out.println(count + "-line paragraph"); if (count > max) { max = count; } } return max; } public static int analyzeParagraphs(Scanner input) { int max = 0; int count = 0; while (input.hasNextLine()) { String line = input.nextLine(); if (line.equals("<p>")) { System.out.println(count + "-line paragraph"); count = 0; } else { count++; } if (count > max) { max = count; } } return max; } 1 header 1 correct while loop (while hasNext or while hasNextLine) 1 reads and stores a line using nextLine() 3 loop that counts lines until "<p>" or if/else that handles "<p>" 1 attempt (inner loop or if/else with some kind of "<p>" test or "<" test) 1 correctly counting number of lines in each paragraph 1 correct comparison (requires equals instead of ==) 1 println for "<count>-line paragraph": printed once for each paragraph, but no double jeopardy with counting correctness and comparison correctness 2 max computation 1 attempt (initialize to something, update inside loop) 1 all correct (initialize to 0, correct update in loop, returned after loop) ------------------------------------------------------------------------------- Problem #6 Solutions: public static boolean isAllPairs(int[] list) { if (list.length % 2 == 1) { return false; } for (int i = 1; i < list.length; i += 2) { if (list[i] != list[i - 1]) { return false; } } return true; } public static boolean isAllPairs(int[] list) { if (list.length % 2 != 0) { return false; } for (int i = 0; i < list.length / 2; i++) { if (list[2 * i] != list[2 * i + 1]) { return false; } } return true; } public static boolean isAllPairs(int[] list) { int pairs = 0; for (int i = 0; i < list.length - 1; i += 2) { if (list[i] == list[i + 1]) { pairs++; } } return list.length % 2 == 0 && pairs == list.length / 2; } 1 header 5 loop that compares adjacent pairs for an even length array 1 loop attempt (some for loop over array length) 1 comparison attempt (comparing list[i]/list[i+1] or list[i]/list[i-1]) or equivalent 1 correct comparison (must be adjacent pair, must test whether the first is not equal to the second) 2 correct structure to compare each successive pair exactly once (mostly atomic, but ask question leader for possible -1 if they are close) 2 handles odd length case 1 reasonable attempt to test for an odd length array 1 correctly returns false for odd length array without throwing an exception 2 true/false/return structure: correctly returns true/false based on a test in a loop (requires some kind of test in a loop, but assume test works). This is generally atomic but can be worth 1 point for solutions that are close. For example, if their only error is that they forgot "return true" after the loop, they get 1 point. If they switch true/false or if they prematurely return true, they lose both points. Ask the question leader if you see something that might be close. ------------------------------------------------------------------------------- Problem #7 Solutions: public static void split(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i += 2) { int n = list.get(i); list.set(i, n / 2 + n % 2); list.add(i + 1, n / 2); } } public static void split(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i += 2) { int n = list.remove(i); list.add(i, n / 2 + n % 2); list.add(i + 1, n / 2); } } 1 header (return type ok if consistent with a return statement, parameter must be ArrayList<Integer>) 4 loop over list 1 attempt (for loop using list.size() or list.length() or list.length) 1 correct bounds (starting at 0, going while < list.size(), not length) 2 increment i by 2 or loop goes backwards 2 expressions/code for splitting list.get(i) into two parts including odd case 1 attempt (testing value % 2, finding value / 2, array syntax okay) 1 correctly splits list.get(i) (must be get, must handle odd case, must be going into structure in correct order in odd case) 3 correctly replaces value with two new values 1 reasonable 2-step attempt using ArrayList methods including: add/add set/set array-style assign followed by add get(i) assign followed by add a single add or single set would not count 2 correct approach using ArrayList methods (set/add, remove/add/add)--can get 1/2 if they sometimes do it right Notes: Saying list.size without parens or using brackets (list.get[i], list.remove[i]) are syntax mistakes. They get one free syntax mistake, max -2 for syntax. -2 for calling set/add/get without saying "list." (only apply this rule if their score goes up) extra data structure goes to question leader Problem #8 Solution: public class Sponge extends Critter { private int dashCount; private int turnCount; public Sponge() { dashCount = 1; turnCount = 0; } public Action getMove(CritterInfo info) { if (info.getFront() == Neighbor.OTHER) { dashCount++; return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else { dashCount = Math.max(dashCount - 1, 1); turnCount++; if (turnCount % 3 == 1) { return Action.LEFT; } else { return Action.RIGHT; } } } public Color getColor() { return Color.YELLOW; } public String toString() { String result = "["; for (int i = 0; i < dashCount; i++) { result = result + "-"; } return result + "]"; } } 1 correct class header 3 declaring fields and constructor 1 field for how many dashes to display (declared, used somewhere) 1 field to count turns or equivalent (declared, modified somewhere) 1 they have a constructor with a correct header that correctly initializes the fields to display "[-]" and to first turn left or their solution has this behavior based on default values for the fields -1 if any field is not private (only lose it once) -1 if they make a field static (only lose it once) 5 correctly updates state 3 increment/decrement a field for dashes (the idea is that they capture the necessary information, not that toString necessarily uses that information correctly) 1 reasonable attempt 2 all correct including not allowing dashes to go below 1 (-1 if close--ask question leader) 2 update a field for turns that implements the length-3 pattern 1 reasonable attempt 1 correct pattern (LEFT/RIGHT/RIGHT and repeat, no double jeopardy if initial move is not LEFT; must be counting turns, not moves) -1 if they update any fields outside of getMove (only take off this point if they earned points for the code); this rule does not apply to fields that are only used by one of the main public methods (e.g., a display string shared by toString and a helper method) 3 toString 2 correct approach: for loop that uses a count field to append dashes or a String field that has dashes appended and substring used to remove dashes (no credit for Python-like expressions "-" * count or for printing) 1 all correct: header, return, including square brackets, pieces all fit together 2 getMove 1 reasonable attempt at header and cases for returning INFECT, HOP, LEFT, RIGHT (all five somewhat correct or four correct) 1 all correct: correct header and correct move (INFECT if enemy in front, HOP if front is EMPTY, LEFT or RIGHT otherwise, no double jeopardy with the length-3 pattern for turns) 1 getColor: correct header and returns Color.YELLOW notes: -1 if they have code after a return (e.g., setting a field); lose this once ------------------------------------------------------------------------------- Problem #9 Solutions: public static int[] surroundWithN(int[] list1, int[] list2, int n) { int[] result = new int[list1.length + 2 * n]; // copy "before" part from list2 for (int i = 0; i < n; i++) { result[i] = list2[i]; } // copy "middle" part--list1 for (int i = 0; i < list1.length; i++) { result[i + n] = list1[i]; } // copy "after" part for (int i = 0; i < n; i++) { result[list1.length + n + i] = list2[i]; } return result; } public static int[] surroundWithN(int[] list1, int[] list2, int n) { int[] result = new int[list1.length + 2 * n]; // copy "before" part from list2 int newIndex = 0; for (int i = 0; i < n; i++) { result[newIndex] = list2[i]; newIndex++; } // copy "middle" part--list2 for (int i = 0; i < list1.length; i++) { result[newIndex] = list1[i]; newIndex++; } // copy "after" part from list2 for (int i = 0; i < n; i++) { result[newIndex] = list2[i]; newIndex++; } return result; } 1 header 2 constructing the new array of appropriate size 1 attempt (construct some new array mostly ignoring syntax) 1 correct (array of int, size correct) 3 setting low range of result ("before" sublist) for (int i = 0; i < n; i++) result[i] = list2[i]; 1 reasonable attempt: * separate loop or if/else branch for this case * assignment and/or index range indicates that they are solving this part of the problem (must be copying from list2 to result) 1 correct number of iterations: n 1 correct copy (-1 if obob or otherwise close) * alignment--first one copied correctly: result[0] * delta is correct: 0 4 setting middle range (list2) for (int i = 0; i < list1.length; i++) result[i + n] = list1[i]; for (int i = n; i < n + list1.length; i++) { result[i] = list1[i - n]; 1 reasonable attempt: * separate loop or if/else branch for this case * assignment and/or index range indicates that they are solving this part of the problem (must be copying from list1 to result) 1 correct number of iterations: list1.length 2 correct copy (-1 if obob or otherwise close) * alignment--first one copied correctly: result[n] * delta is correct: -n 4 setting high range ("after" sublist) for (int i = 0; i < n; i++) { result[list1.length + n + i] = list2[i]; for (int i = n + list1.length; i < list1.length + 2 * n; i++) { result[i] = list2[i - list1.length - n]; 1 reasonable attempt: * separate loop or if/else branch for this case * assignment and/or index range indicates that they are solving this part of the problem (must be copying from list1 to result) 1 correct number of iterations: n 2 correct copy (-1 if obob or otherwise close) * alignment--first one copied correctly: result[n + list1.length] * delta is correct: -list1.length - n 1 returns their answer (requires construct attempt) Notes: ArrayList solutions or extra data structures solutions go to question leader Syntax errors are capped at -2. For ArrayList, eligible for: header, loop attempt/bounds, return (max 8) -3 off the top if they switched list1/list2 (only use this rule if their score goes up) Problem #10 Solutions: public static String acronym(String s) { boolean inWord = false; s = s.toUpperCase(); String result = ""; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == ' ' || ch == '-') { inWord = false; } else if (!inWord) { inWord = true; result += ch; } } return result; } public static String acronym(String s) { String s2 = ""; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '-') { s2 += " "; } else { s2 += ch; } } Scanner input = new Scanner(s2.toUpperCase()); String result = ""; while (input.hasNext()) { result += input.next().charAt(0); } return result; } ------------------------------------------------------------------------------- Can get up to 3 points for any of the following good things: 1 correct header 1 for loop using string length 1 getting one char from source string with charAt or substring(i, i + 1) 1 testing whether a character in the source string is a space or dash 1 initializing a local string to an empty string 1 appending a character from source string to local string 1 uppercasing the source string or a local string 1 returning a local string ------------------------------------------------------------------------------- To go to round 2, a solution needs one of the following: * testing adjacent characters for space/dash versus !space/dash * constructing a Scanner from the string, calling next ------------------------------------------------------------------------------- Without replacing dashes with spaces, the Scanner solution gets 5 points Deductions: -3 major error -2 medium error && vs || need to delete nontrivial code of theirs fails on delimiters composed of just dashes fails on strings that begin with a letter fails on strings that end with a dash -1 minor error OBOB (you can fix code by changing one expression by +1 or -1) charAt or length instead of s.charAt, s.length testing whether character is null instead of ' ' didn't return result -0 minor syntax errors String result = s.charAt(0); String new = ... (reserved word) s.length vs s.length() string/char confusion (==, .equals) -4 requires mutable strings...attempting to do String replace for a Scanner solution by saying something like: s.charAt(i) = ' '; -------------------------------------------------------------------------------
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011