Problem #1
Expression Value
---------------------------------------------------
43 % 15 / 3 + 15 / 2 11
6.2 * 5 / 10 + 3.5 6.6
6 * 2.5 / 4 + (2.3 + 2.7) / 4 5.0
"18" + 3 * 4 + (8 + 5) "181213"
59 % 10 / (2 + 2) * 2.5 / 2 2.5
1 point each, type has to match (2.0 vs 2, using quotes for string)
-------------------------------------------------------------------------------
Problem #2:
Original List Final List
--------------------------------------------------------------
{8, 3} {9, 3}
{2, 5, 4} {3, 5, 4}
{3, 4, 5, 7} {3, 5, 5, 7}
{2, 5, 4, 2} {3, 5, 5, 2}
{4, 5, 7, 9, 3} {5, 5, 7, 9, 3}
2 points for each answer
-1 for each wrong number
-1 for each missing number
any given answer can't go negative
-------------------------------------------------------------------------------
Problem #3:
b b c b
c 1 c 1 c 1 d 1
a 2 b 2 c 2 b 2
1/2 point each, round up their score
-1 if columns and/or rows are labeled but in the right order
-2 if columns and/or rows are labeled and in the wrong order
-1 per extra column or row (max -2)
-1 if two columns transposed or two rows transposed
-2 if you need to transpose twice
-2 if rows and columns are transposed
-0 for quotes or lack of spaces or capital letters
-0 for solid lines between that separate columns or rows
-------------------------------------------------------------------------------
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)
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
Solution:
public static void underline(Scanner input) {
while (input.hasNextLine()) {
String text = input.nextLine();
if (!text.startsWith(".")) {
System.out.println(text);
} else {
System.out.println(text.substring(1));
for (int i = 1; i <= text.length() - 1; i++) {
System.out.print("-");
}
System.out.println();
}
}
}
1 header
1 correct while loop (while hasNextLine)
1 reads and stores a line using nextLine()
1 sometimes echos a line with System.out.println (requires reading point)
5 correctly handles lines with dots
1 reasonable attempt to test for dot (startsWith or charAt/substring to
access first character)
1 reasonable attempt to print all but dot (substring or loop)
1 reasonable attempt to print line of dashes (for loop printing dashes)
1 at least 2 of these are correct
1 all three are correct and non-dot lines are echoed correctly and no
extraneous output (this is the nit-picky uber-correctness point)
Notes:
- charAt/substring throw an exception for empty strings (blank lines)--this
is part of correctness
- It will be common for them to be off by one for the number of dashes
(length of original line versus line minus the dot)--this is part of
correctness
- Ignore any code for special case of a line with just a period.
Problem #6
Solution:
public static boolean isFibLike(int[] list) {
for (int i = 2; i < list.length; i++) {
if (list[i] != list[i - 2] + list[i - 1]) {
return false;
}
}
return true;
}
1 header
3 for loop over array
1 attempt (for loop that iterates around list.length times)
1 loop iterates (list.length-1) or (list.length-2) or (list.length-3) times
1 loop iterates exactly (list.length-2) times
4 testing fib properly (just the test)
1 attempt (comparing something1 against something2 + something3)
3 correct (list[i + 2] == list[i] + list[i + 1] or equivalent; -1 if there
is a small discrepancy with loop variable like an OBOB)
2 correctly returns true/false based on fib test (requires test attempt, but
otherwise 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 can get 1 of these points. 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.
Note:
We generally ignore code like this:
if (list.length <= 2) {
return true;
}
unless it screws up an otherwise correct answer, in which case they lose 1
point off the top.
Problem #7
Solutions:
// expand going forward, have to increment by n
public static void expand(ArrayList list) {
int i = 0;
while (i < list.size()) {
int times = list.remove(i);
int n = list.remove(i);
for (int j = 0; j < times; j++) {
list.add(i, n);
}
i += times;
}
}
// remove each pair as you go, append new values at the end
public static void expand(ArrayList list) {
int pairs = list.size() / 2;
for (int i = 0; i < pairs; i++) {
int times = list.remove(0);
int n = list.remove(0);
for (int j = 0; j < times; j++) {
list.add(n);
}
}
}
// append new values to the end, delete pairs after
public static void expand(ArrayList list) {
int oldSize = list.size();
for (int i = 0; i < oldSize; i += 2) {
int times = list.get(i);
int n = list.get(i + 1);
for (int j = 0; j < times; j++) {
list.add(n);
}
}
for (int i = 0; i < oldSize; i++) {
list.remove(0);
}
}
// expand going backwards
public static void expand(ArrayList list) {
for (int i = list.size() - 1; i > 0; i -= 2) {
int times = list.get(i - 1);
int n = list.get(i);
for (int j = 0; j < times - 1; j++) {
list.add(i, n);
}
list.remove(i - 1);
}
}
1 header (return type ok if consistent with a return statement, parameter must
be ArrayList)
3 loop over pairs
1 attempt (loop using list.size() or list.length() or list.length)
1 better attempt: uses list.size(), either goes backwards or goes forward
and adds something related to n
1 correct: uses list.size() and correctly repositions to next pair
4 loop to add values
1 reasonable attempt to get count and value (array syntax okay)
1 correctly gets count and value
1 attempt to loop n times adding a value (requires loop, call on add)
1 correctly adds occurrences of value so that there are n of them (no
double jeopardy with get)
2 remove count
1 reasonable attempt
1 correct
Notes:
Saying list.size without parens or using brackets on method calls (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/remove without saying "list." (only apply this rule
if their score goes up)
extra data structure goes to question leader
Problem #8
Solution:
public class Salmon extends Critter {
private Random r;
private String display;
public Salmon() {
r = new Random();
display = "<*>";
}
public Action getMove(CritterInfo info) {
if (info.getFront() == Neighbor.OTHER) {
return Action.INFECT;
} else if (info.getFront() == Neighbor.EMPTY) {
return Action.HOP;
} else if (r.nextInt(2) == 0) {
display = "";
return Action.LEFT;
} else {
display = "";
return Action.RIGHT;
}
}
public Color getColor() {
return Color.PINK;
}
public String toString() {
return display;
}
}
1 class header
2 fields/constructor
1 display or other field that keeps track of most recent turn
1 Random object that they construct or they used Math.random()
6 toString
1 header
2 returns "<*>" initially based on a field
3 changes correctly after a turn is made
1 attempt: sets a value that leads to returning ""/""
2 correct: correctly returns "" and "" after turns (-1 if it
changes back to "<*>" after a non-turn like infect or hop)
5 getMove
1 header
1 returns INFECT whenever enemy is in front
1 returns HOP whenever front is empty
2 randomly choose between LEFT/RIGHT when not infecting or hopping
1 attempt: attempt to use randomness to choose between LEFT and RIGHT
1 correct (lose this if using Math.random and do it wrong like leaving
off parens)
1 getColor
overal deductions:
-1 if they have code after a return (e.g., setting a field)
-1 if any field is not private
-1 if any field set to non-default outside constructor
-1 if they update any fields outside of getMove
-1 if they have a local variable in their constructor that shadows a field
(they can only lose this once)
special rule:
if they use a local variable as a field, they lose the field point always,
but you can take off -2 and preted it's a field if that raises their score
Problem #9
Solutions:
public static int[] insert(int[] list1, int[] list2, int index) {
int[] result = new int[list1.length + list2.length];
for (int i = 0; i < index; i++) {
result[i] = list1[i];
}
for (int i = 0; i < list2.length; i++) {
result[index + i] = list2[i];
}
for (int i = index; i < list1.length; i++) {
result[list2.length + i] = list1[i];
}
return result;
}
public static int[] insert(int[] list1, int[] list2, int index) {
int[] result = new int[list1.length + list2.length];
for (int i = 0; i < result.length; i++) {
if (i < index) {
result[i] = list1[i];
} else if (i < index + list2.length) {
result[i] = list2[i - index];
} else {
result[i] = list1[i - list2.length];
}
}
return result;
}
2 header (-1 per error)
3 constructing the new array of appropriate size
1 attempt (construct some new array mostly ignoring syntax)
1 better attempt (array of int, length is within 2 of correct length)
1 correct (array of int, size correct)
3 setting low range:
assignment i start i stop
-----------------------------------------
result[i] = list1[i] 0 index
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)
2 correct (-1 if obob or otherwise close)
3 setting middle range:
assignment i start i stop
---------------------------------------------------------------
result[i] = list2[i - index] index index + list2.length
result[i + index] = list2[i] 0 list2.length
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)
2 correct (-1 if obob or otherwise close)
3 setting high range:
assignment i start i stop
---------------------------------------------------------------------------
result[i] = list1[i - list2.length] index + list2.length result.length
result[i + list2.length] = list1[i] index list1.length
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)
2 correct (-1 if obob or otherwise close)
1 returns their answer (requires construct attempt)
Notes:
Students weren't supposed to use ArrayList or Arrays.copyOf or
Collections.reverse--when in doubt, show to question leader
Syntax errors are capped at -2.
ArrayList solutions were allowed to get:
1 construct attempt (construct a new ArrayList)
1 ArrayList equivalent of result[something] = list1[something]
1 ArrayList equivalent of result[something] = list2[something]
1 ArrayList equivalent of loop that leads to 2 * result.length assignments
to result
1 returns their answer after a for loop
Problem #10
Solutions:
public static String encode(String s, int n) {
String result = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < s.length() - i; j += n) {
result += s.charAt(i + j);
}
}
return result;
}
public static String encode(String s, int n) {
String result = "";
for (int i = 0; i < n; i++) {
for (int j = i; j < s.length(); j += n) {
result += s.charAt(j);
}
}
return result;
}
public static String encode(String s, int n) {
String result = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < s.length(); j++) {
if (j % n == i) {
result += s.charAt(j);
}
}
}
return result;
}
(not very good) Plus criteria (max +4)
Good things that each get 1 point:
- Header
- s.charAt or s.substring(1 long)
must be S.
- loop to s.length() OR loop to n
- Result String declared and initialized
- Result string returned
- result += something (can be anything)
- index += n
-------------------------------------------------------------------------------
(pretty good) Minus criteria (min + 4)
Round 2 Criteria - Must have these to pass to round 2
-----------------------------------------------------
- charAt or substring
- string concatenation
- loop that increments an index by n
- inner loop or equivalent that goes over length of string
Deductions:
-1 minor error:
OBOB in loop bounds
no return
confused identifier
indexOf instead of charAt
OBOB in substring
+/= instead of "+="
not having "s."
spaces between element
-2 medium error:
inner loop starts at 0 instead of i
inner loop starts at n instead of n + i
incorrect mod forumula (off by a variable)
need to cross off code
= instead of +=
using string as Scanner
increment while loop counter at wrong place
-3 major error:
inner/outer loops switched
missing final group (fails when string length % n != 0)
not concatenating using +=, e.g., inventing string methods
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011