Problem #1
Expression Value
-----------------------------------------------
3 + 3 * 8 - 2 25
109 % 100 / 2 + 3 * 3 / 2.0 8.5
1 - 3 / 6 * 2.0 + 14 / 5 3.0
1 + "x" + 11 / 10 + " is" + 10 / 2 1x1 is5
10 % 8 * 10 % 8 * 10 % 8 0
2 points each, 1 point for correct value of wrong type (missing quotes, no .0)
-------------------------------------------------------------------------------
Problem #2:
solution:
she can't take she with her
it can't take her with you
you can't take it with you
fred can't take her with him
1 point for each of the 12 values
-1 off the top if any error in non-parameter text (no dittos, quotes, labels)
-4 if two columns transposed
-8 if you need to transpose twice
okay to use underscore for space
-------------------------------------------------------------------------------
Problem #3:
Method Call Output Produced
---------------------------------------
ifElseMystery(5, 15); 7 15
ifElseMystery(12, 5); 12 1
ifElseMystery(7, 8); 8 9
ifElseMystery(2, 3); 4 -2
ifElseMystery(1, -2); 2 -7
ifElseMystery(1, 1); 2 -4
each number is worth a point
-2 overall if columns transposed
-1 off the top if formatting error (e.g., commas between numbers, quotes)
okay to use underscore for space
-------------------------------------------------------------------------------
Problem #4:
4. Method Call Output Produced
---------------------------------------
mystery(1); 1
mystery(3); 2
mystery(5); 5
mystery(7); 13
3 points each
-3 off the top if fibonacci sequence is one off (e.g., 1, 1, 3, 8)
Problem #5:
x > 2 x < n n % x == 0
+---------------------+---------------------+---------------------+
Point A | never | sometimes | sometimes |
+---------------------+---------------------+---------------------+
Point B | sometimes | always | sometimes |
+---------------------+---------------------+---------------------+
Point C | never | sometimes | sometimes |
+---------------------+---------------------+---------------------+
Point D | always | sometimes | sometimes |
+---------------------+---------------------+---------------------+
Point E | sometimes | never | sometimes |
+---------------------+---------------------+---------------------+
1 point per answer
Problem #6
Solution:
public static boolean checkPrime(int n) {
System.out.print("factors of " + n + " = 1");
int count = 1;
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
System.out.print(", " + i);
count++;
}
}
System.out.println();
System.out.println("Total factors = " + count);
return count == 2;
}
1 correct header
2 correct loop structure (ignoring fencepost issue and early return):
1 basic attempt: a loop that has bounds mostly going 1 to n
1 correct: a single for loop that gives a variable i the values 1 through n
or the equivalent in while loop form (okay to handle the special case of
printing 1 before the loop and/or n after the loop, lose it if they have
nested loops that produce too much output or other odd structures)
2 counting factors
1 attempt: (incrementing a counter with code that finds or prints a factor)
OR (counter initialized before a loop and printed after a loop)
1 correct ignoring issues of loop structure and early return: correct test
for factor, correct initialization for counter, correctly increments in
an if or equivalent
3 producing output (ignoring fencepost issue and early return):
1 reasonable attempt for at least two of the three printing tasks:
print n once before any loop along with the intro
print some variable that could be a factor inside some loop
print total factors once with most of the text after a loop
1 reasonable attempt for all three printing tasks
1 all elements are printed and all formatting correct including using print
commands for the elements of the first line of output along with an empty
println to complete the line, a println used to produce the second line
of output with the factor count, an indication of spacing in their string
constants, no extra output (blank line at the end is okay)
1 returns true when number of factors is exactly 2, returns false when it is
not, lose this point if they return early, no double jeopardy with earlier
code--this is about what they return assuming their counter has the correct
value
1 solved the fencepost: clear indication of either printing first term before a
loop or printing last term after a loop, printing other terms inside a loop
with leading comma (if first term before loop) or trailing comma (if last
term after loop), no extra commas printed
Problem #7
solution:
public static void printDuplicates(Scanner input) {
String prevToken = input.next();
int count = 1;
while (input.hasNext()) {
String nextToken = input.next();
if (nextToken.equals(prevToken)) {
count++;
} else if (count > 1) {
System.out.println(prevToken + "*" + count + " ");
count = 1;
}
prevToken = nextToken;
}
if (count > 1) {
System.out.println(prevToken + "*" + count + " ");
}
}
1 header (non-void return type ok if consistent with a return statement)
1 correct loop (testing on hasNext)
6 counting sequences
1 reasonable attempt to initialize a counter and increment in a loop
1 reasonable attempt to compare two consecutive tokens for equality
1 count is updated correctly (initially 1, reset to 1 when change is seen)
1 correct test (two consecutive tokens tested with equals and not ==)
2 correct setup to recognize sequences (read one token outside loop, read
exactly one inside the loop, remember previous token
2 printing
1 reasonable attempt to print duplicate once inside loop
1 all printing correct (correct format, prints once when transition is seen
assuming tests worked)
1 correctly handles last sequence and all printing is done correctly (generally
requires correct setup)
Problem #8
Solutions:
// start at index 0 and compare list[i]/list[i+1], adding 2 to i
// requires "list.length - 1" or equivalent in test to handle odd case
public static boolean isPairwiseSorted(int[] list) {
for (int i = 0; i < list.length - 1; i += 2) {
if (list[i] > list[i + 1]) {
return false;
}
}
return true;
}
// start at index 0 and compare list[i]/list[i+1], adding 1 to i
// requires test on i % 2 == 0 so they don't test all pairs
// requires "list.length - 1" or equivalent in test to handle odd case
public static boolean isPairwiseSorted(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
if (i % 2 == 0 && list[i] > list[i + 1]) {
return false;
}
}
return true;
}
// start at index 1 and compare list[i]/list[i-1], adding 2 to i
// does NOT require special adjustment to length test
public static boolean isPairwiseSorted(int[] list) {
for (int i = 1; i < list.length; i += 2) {
if (list[i] < list[i - 1]) {
return false;
}
}
return true;
}
// start at index 0 and compare list[2*i]/list[2*i+1], adding 1 to i
// requires testing list.length/2, but no special test for odd length
public static boolean isPairwiseSorted(int[] list) {
for (int i = 0; i < list.length / 2; i++) {
if (list[2 * i] > list[2 * i + 1]) {
return false;
}
}
return true;
}
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 that the first is
greater than 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 some attempt to test or adjust for a possible odd length array or they
had an approach like using list.length/2 that doesn't require adjustment)
1 correctly handles odd length array without throwing an exception
(requires that they are comparing adjacent elements and that an odd
element at the end does not lead to an extra comparison, but no double
jeopardy with test correctness)
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. Or their only error might be to fail to return true
for an array of length less than 2, in which case 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 #9
Solutions:
public static boolean samePattern(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
for (int i = 0; i < s1.length(); i++) {
for (int j = i + 1; j < s1.length(); j++) {
if (s1.charAt(i) == s1.charAt(j) &&
s2.charAt(i) != s2.charAt(j)) {
return false;
}
if (s2.charAt(i) == s2.charAt(j) &&
s1.charAt(i) != s1.charAt(j)) {
return false;
}
}
}
return true;
}
public static boolean samePattern(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
for (int i = 0; i < s1.length(); i++) {
for (int j = i + 1; j < s1.length(); j++) {
if ((s1.charAt(i) == s1.charAt(j)) !=
(s2.charAt(i) == s2.charAt(j))) {
return false;
}
}
}
return true;
}
public static boolean samePattern(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
for (int i = 0; i < s1.length(); i++) {
if (s1.indexOf(s1.charAt(i)) != s2.indexOf(s2.charAt(i))) {
return false;
}
}
return true;
}
public static boolean samePattern(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
String chars1 = "";
String chars2 = "";
for (int i = 0; i < s1.length(); i++) {
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
if (chars1.indexOf(ch1) != -1) {
if (chars1.indexOf(ch1) != chars2.indexOf(ch2)) {
return false;
}
} else {
if (chars2.indexOf(ch2) != -1) {
return false;
}
chars1 += ch1;
chars2 += ch2;
}
}
return true;
}
-------------------------------------------------------------------------------
Round 1
Can get up to 4 points for any of the following good things:
1 correct header
1 testing whether lengths are equal/unequal
1 returning false when lengths are unequal
1 for loop using string length
1 getting one char from str1 with charAt or substring(i, i + 1)
1 getting one char from str2 with charAt or substring(i, i + 1)
1 returning false in a loop for some kind of character comparison
1 returning true at the end of the method
-------------------------------------------------------------------------------
To go to round 2, a solution needs:
* a nested loop approach
* calls on indexOf
* building up strings
-------------------------------------------------------------------------------
Round 2 Deductions
-3 major error
-2 medium error
&& vs ||
need to delete nontrivial code of theirs
-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 ' '
-0 minor syntax errors
String result = s.charAt(0);
String new = ... (reserved word)
s.length vs s.length()
string/char confusion (==, .equals)
We decided that for this problem, checking in only one direction was -3 and not
checking length was -2.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011