Problem #1
2 [12, 8, 5]
5 [2, 2, 5]
16 3
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 {} instead of []
-1 off the top if a non-quote formatting error (underscores okay for spaces)
-------------------------------------------------------------------------------
Problem #2:
Original List Final List
--------------------------------------------------------------
{2, 5, 6, 9} {2, 3, 5, 9}
{1, 3, 3, 5, 8} {1, 2, 4, 7, 8}
{4, 4, 5, 8, 2} {4, 4, 6, 9, 2}
{3, 6, 9, 12, 15} {3, 4, 6, 9, 15}
{2, 4, 6, 8, 10, 12} {2, 3, 5, 8, 12, 12}
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 d
b 1 b 1 d 1 c 1
b 2 b 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 tallyScores(Scanner input) {
while (input.hasNext()) {
String name = input.next();
System.out.print(name + ":");
int points = 0;
int count = 0;
while (input.hasNextInt()) {
int nextPoints = input.nextInt();
points += nextPoints;
count++;
System.out.print(" " + nextPoints);
}
System.out.println();
double average = (double) points / count;
System.out.println("average = " + average);
}
}
1 header (non-void return type ok if consistent with a return statement)
2 outer while loop that prints name
1 reasonable attempt: two of the following:
while loop exists
test is hasNext()
reads one string with input.next()
prints name (doesn't require colon)
1 all four of these are correct
3 inner while loop that reads ints and prints them
1 reasonable attempt at loop (has to be a different loop than previous)
1 reasonable attempt to read and print an int
1 all correct: test is hasNextInt, reads exactly one int using nextInt,
stores value in a variable, prints value (doesn't require a space)
3 average computation
1 reasonable attempt to be able to compute the average of the scores: 3 of
the following
initialize counter to 0
initialize sum to 0
increment counter each time they read a score
increment sum by next score
1 reasonable attempt to compute average (sum / count) and print it
1 all correct other than formatting (requires conversion to double and
correct resetting of sum and count for each new student)
1 prints all three elements (name, score, average) with correct formatting:
correct use of print/println, spaces between numbers, all formatting is
correct, elements are printed in correct order
Notes:
a student who stores values as a double loses the final printing point and
also loses the inner loop correctness if they also use nextDouble instead of
nextInt
-------------------------------------------------------------------------------
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 (non-void return type ok if consistent with a return statement)
3 loop that reads each line of input file exactly once
1 correct while loop (while hasNextLine)
1 reasonable attempt to read/store a line and print it
1 correctly reads/stores exactly one line each time through loop without
missing any input lines and correctly prints lines without dots
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 int numUnique(int[] list) {
if (list.length == 0) {
return 0;
} else {
int count = 1;
for (int i = 1; i < list.length; i++) {
if (list[i] != list[i - 1]) {
count++;
}
}
return count;
}
}
1 header
6 loop that correctly counts unique values for nonempty arrays
1 loop attempt: loop using array length referring to list[something]
1 reasonable attempt to compare list[i]/list[i+1] or list[i]/list[i-1]
1 correct loop bounds to compare all adjacent pairs without going out of
bounds
1 counter correctly initialized outside loop (usually set to 1, but pay
attention to what they are returning)
2 correct comparison (whether adjacent values are not equal) used to
increment a counter (don't worry about counter initialization for these
points)
2 correctly handles array of length zero (requires separate test)
1 reasonable attempt to handle this case as a separate case
1 correctly tests for this case and returns 0
1 returns counter after loop
-------------------------------------------------------------------------------
Problem #7
Solutions:
// forwards loop starting at index 1
public static void rearrange(ArrayList list) {
for (int i = 1; i <= list.size() / 2; i++) {
int n = list.get(i);
list.remove(i);
list.add(n);
}
}
// forwards loop that adds the values at even indexes at the front
public static void rearrange(ArrayList list) {
int count = 0;
for (int i = 1; i < list.size(); i++) {
if (i % 2 == 0) {
count++;
int move = list.remove(i);
list.add(count, move);
}
}
}
// backwards loop with i -= 2
public static void rearrange(ArrayList list) {
int count = 0;
for (int i = list.size() - 1 - list.size() % 2; i >= 0; i -= 2) {
count++;
list.add(list.size() - count, list.remove(i));
}
}
// variation of forwards loop that uses a count instead of size/2
public static void rearrange(ArrayList list) {
int count = 0;
for (int i = 1; i < list.size() - count; i++) {
list.add(list.remove(i));
count++;
}
}
// weird student solution that works
public static void rearrange(ArrayList list) {
int count = 0;
for (int i = 0; i < list.size() - count; i++) {
if (i % 2 != 0) {
int num = list.get(i);
list.add(num);
count++;
}
}
for (int i = 1; i <= count; i++) {
list.remove(i);
}
}
1 header (return type ok if consistent with a return statement, parameter must
be ArrayList)
5 correct loop structure to remove values originally at even indexes
1 loop attempt (loop using list.size() or list.length() or list.length)
2 loop is structured so that it will move size/2 values (-1 if OBOB or one
other minor mistake like saying list.length, can get this if their remove
is wrong, as in list.set)
2 correct approach and pieces fit together (can get this even if the loop
iterates too many times; usually atomic, but ask question leader if
someone is close)
start at index 1, move i, i++
move values at index 2, 4, 6, ..., to front with counter for # moved
4 moving a value to another part of the list
1 reasonable attempt to remove a value (can be "remove" instead of
"list.remove")
1 reasonable attempt to either store the value being removed in a variable
or they use the call on remove as their parameter for add, as in:
int n = list.get(i); list.remove(i); list.add(n);
int n = list.remove(i); list.add(n);
list.add(list.remove(i));
(can use array notation instead of list.get, can get this even if they
are calling list.set))
1 reasonable attempt to add removed value back into list (can be "add"
instead of "list.add")
1 all correct (does move a value from one spot to another in the list
without losing any values)
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.
extra data structure goes to question leader
use of list.set that might work goes to question leader
Problem #8
Solution:
public class Meeseeks extends Critter {
private int count;
private Random r;
private String display;
public Meeseeks() {
count = 0;
r = new Random();
setDisplay();
}
public Action getMove(CritterInfo info) {
count++;
setDisplay();
if (info.getFront() == Neighbor.EMPTY) {
return Action.HOP;
} else if (info.getFront() == Neighbor.SAME) {
return Action.LEFT;
} else {
return Action.INFECT;
}
}
public Color getColor() {
return Color.BLUE;
}
public String toString() {
return display;
}
private void setDisplay() {
if (count % 4 == 0) {
int choice = r.nextInt(2);
if (choice == 0) {
display = "mister";
} else {
display = "meeseeks";
}
}
}
}
1 correct class header
4 declaring fields and constructor
1 field to count moves/cycle or equivalent (declared, modified somewhere)
1 field for Random object (declared, used somewhere)
1 field for tracking what string to display (declared, used somewhere)
1 they have a constructor with a correct header that correctly initializes
a Random object and that includes any other initialization to non-default
values
-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
1 reasonable attempt to count moves (all moves or moves % 4)
1 reasonable attempt to set a second field every four moves to a random
50/50 choice
2 correctly updates their fields so that every four moves they make a
random 50/50 choice that they record in a field (don't worry about what
happens in the initial moves; probably atomic, but ask question leader if
they seem close)
1 overall set up is correct to make a random choice before the first move
and then to make a new choice just before moves 4, 8, 12, etc.
-1 if they update any fields outside of getMove (only take off this point
if they earned points for the code)
2 toString
1 reasonable attempt at an if/else to sometimes return "mister" and
sometimes return "meeseeks" ("meeseeks." is okay; code for this might be
elsewhere if they are returning a display field, as in the sample)
1 all correct (correct header, has code that would sometimes return one
string and sometimes return the other if fields are updated properly)
2 getMove
1 reasonable attempt at header and cases for returning HOP, LEFT, INFECT
(probably all four somewhat correct or three correct)
1 all correct: correct header and correct move (HOP if front is EMPTY, LEFT
if front is SAME, else INFECT)
1 getColor: correct header and returns Color.BLUE
notes:
-1 if they have code after a return (e.g., setting a field)
-------------------------------------------------------------------------------
note after: I adjusted the criteria to give fewer points for header and
constructing the array and put them into atomic correctness points. This
should be reworked to have more credit for things like having list[i]+list[i+1]
somewhere in their code.
Problem #9
Solutions:
public static int[] collapse(int[] list) {
int[] result = new int[list.length / 2 + list.length % 2];
for (int i = 0; i < list.length; i++)
result[i / 2] += list[i];
return result;
}
public static int[] collapse(int[] list) {
int size = list.length / 2;
if (list.length % 2 == 1) {
size++;
}
int[] result = new int[size];
for (int i = 0; i < list.length / 2; i++) {
result[i] = list[i * 2] + list[i * 2 + 1];
}
if (list.length % 2 == 1) {
result[result.length - 1] = list[list.length - 1];
}
return result;
}
1 header
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)
7 correctly collapses arrays of even length
1 loop over their result array setting result[i] to something
2 setting result[i] to sum of adjacent values from list
4 correct (this is the hard part--using /2 or *2 or equivalent to match up
appropriate indexes between the two arrays; mostly atomic, but -1 if
OBOB)
3 correctly handles odd case (sets last element of result to last element of
list)
1 returns their answer after a for loop
Notes:
extra data structures go to question leader
ArrayList solutions were allowed to get:
1 construct attempt (construct a new ArrayList)
1 ArrayList equivalent of loop over their result array setting result[i] to
something
2 ArrayLislt equivalent of setting result[i] to sum of adjacent values from
list
1 returns their answer after a for loop
Notes:
ArrayList solutions or extra data structures solutions go to question leader
Syntax errors are capped at -2.
Problem #10
Solutions:
public static void sumCount(int[] data, int[] sum, int[] count) {
for (int i = 0; i < 4; i++) {
sum[i] = 0;
count[i] = 0;
}
for (int i = 0; i < data.length; i++) {
int index = (i % 7 + 1) / 2;
if (data[i] > 0) {
count[index]++;
sum[index] += data[i];
}
}
}
public static void sumCount(int[] data, int[] sum, int[] count) {
for (int i = 0; i < 4; i++) {
sum[i] = 0;
count[i] = 0;
}
for (int i = 0; i < data.length; i++) {
if (data[i] > 0) {
if (i % 7 == 0) {
count[0]++;
sum[0] += data[i];
} else if (i % 7 == 1 || i % 7 == 2) {
count[1]++;
sum[1] += data[i];
} else if (i % 7 == 3 || i % 7 == 4) {
count[2]++;
sum[2] += data[i];
} else {
count[3]++;
sum[3] += data[i];
}
}
}
}
1 header
2 setting counters to 0 before tallying or resetting them to local counters
after tallying
1 reasonable attempt (Arrays.fill can get attempt, but not correct)
1 correct for both sum and count arrays
4 generous pattern match
1 reasonable attempt to loop over data array
2 increments count[something] when data[i] > 0 (or updates local variable
that is later stored in count array)
1 reasonable attempt (can get this if local variables updated but not
copied back to array)
1 correct
1 adds data[i] to sum[something] (or updates local variable that is later
stored in count array; can be every time)
3 updates correct counter(s) for each element of data array
1 reasonable attempt
2 correct loop bounds to access each element of data array exactly once,
turning the data array index into the appropriate index for the count
arrays (okay if they update only one of the count arrays, but if they
update both, it has to match; -1 if minor mistake like loop OBOB)
-------------------------------------------------------------------------------
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011