Anda di halaman 1dari 5

CS106B

Summer 2012

Handout #17G
July 23, 2012

CS106B Midterm Grading Criteria


Problem 1: 15 points
a) 4

2: If they get the correct answer

b) 2*(2*3 + 2*2 + 2*1) + 0 = 24


4: If they get the correct answer
c) O(n)
4: If they get the correct answer
d) O(n2)
5: If they're answer is correct OR if O(n) times their answer in (c).
Problem 2: 10 points
string customizeTemplate(string template, Map<string,string>
&tokens) {
string result = ;
while (true) {
int startIndex = template.find(();
if (startIndex == string::npos) {
break;
}
int endIndex = template.find());
string key = template.substr(startIndex+1,
endIndex-startIndex-1);
result += template.substr(0,startIndex) + tokens[key];
template = template.substr(endIndex+1);
}
return result;
}

2/5
Point Breakdown:
1: Getting location of key
Amounts to just finding the open and close parentheses
3: Extracting key
Only give them 1 point if they include the parentheses
2: Adding text up to key to result
Be careful to not include open parenthesis
2: Adding tokens[key] to result
2: Advancing beyond the token we just processed
This can take multiple forms. In the solution above, it involves removing the prefix
up to endIndex+1. If they used a StringTokenizer, then this is taken care of
by the class. Another valid solution would be to maintain an integer that represents
a the end of the last token that was processed, and use this to when calling
substr() and find()
Problem 3: 10 points
int computeScore(Grid<bool> &board, int row, int col) {
int score = 2; //points for (row,col)
for (int dRow = -1; dRow <= 1; dRow++) {
for (int dCol = -1; dCol <= 1; dCol++) {
if (abs(dRow) + abs(dCol) == 2 ||
(dRow == 0 && dCol == 0)) continue;
int currRow = row + dRow;
int currCol = col + dCol;
while (board.inBounds(currRow,currCol) &&
board[currRow][currCol]) {
currRow += dRow;
currCol += dCol;
score++;
}
}
}
return score;
}
Notes: It isn't clear from the problem whether or not the board[row][col] is true or
false (i.e. whether or not the move has already been made) they are welcome to make
either assumption as long as they are consistent.
Point Breakdown:
6: Iterating over cells and incrementing score in all 4 cardinal directions
2: considers all 4 directions
2: resets currRow and currCol to row and col respectively when starting the
search in a new direction

3/5

2: Count (row,col) exactly twice when computing score.


2: Stop iterating in a direction if off of grid
2: Stop iterating in a direction if board[currRow][currCol] is false

Other Deductions:
-2: board is in a modified state when the function returns (unless they decided to set
board[currRow][currCol] = true; I don't care if they did that).
-2: they count tiles in the diagonal directions
Problem 4: 10 points
Notes:
I don't care about the case of the palindromes
The problem was a little unclear about whether a palindrome has to be an English
word. Don't mark them down if they add a check to see if the palindrome is an
English word before printing it.
void printAllPalindromes(int numChars) {
//Build all even length palindromes
printAllPalindromes(,numChars);
//all odd length palindromes
for (char ch = 'a'; ch <= 'z'; ch++) {
printAllPalindromes(string(ch),numChars);
}
}
void printAllPalindromes(string soFar, int numChars) {
if (soFar.length() > numChars)
return;
cout << soFar << endl;
for (char ch = 'a'; ch <= 'z'; ch++) {
printAllPalindromes(ch + soFar + ch,numChars);
}
}
Point Breakdown:
3: Makes initial recursive calls on empty string AND all 1 character strings
Only give them 1 point if they miss either
1: prints permutation being build up
2: Base Case stops recursing if length is greater than numChars
4: Recursive Decomposition
1: Iterates over every letter
2: Builds new string consisting of soFar surrounded by the current letter
1: Properly updates and passes numChars. In the solution above, no update to
numChars was necessary, but some solutions may require it.

4/5
Problem 5: 15 points
bool canSeat(Map<string,Set<string> > &enemies, int numTables) {
Vector<Set<string> > tables;
Vector<string> friends;
foreach (string friend in enemies) {
friends += friend;
}
return canSeat(enemies, numTables, tables, friends);
}
bool canSeat(Map<string,Set<string> > &enemies, int numTables,
Vector<Set<string> > tables,
Vector<string> friendsLeft) {
if (tables.size() > numTables)
return false;
if (friendsLeft.size() == 0)
return true;
string currFriend = friendsLeft[0];
friendsLeft.removeAtIndex(0);
//Try adding friend to an existing table
for (int i = 0; i < tables.size(); i++) {
Vector<Set<string> > table = tables[i];
bool tableHasEnemy = false;
for (string possibleEnemy in table) {
if (enemies[currFriend].contains(possibleEnemy)) {
tableHasEnemy = true;
break;
}
}
if (!tableHasEnemy) {
Vector<Set<string> > tablesCopy = tables;
tablesCopy[i] += currFriend;
if (canSeat(enemies,numTables,tablesCopy,friendsLeft)) {
return true;
}
}
}
//Try creating new table for friend
Set<string> newTable;
newTable += currFriend;
tables += newTable;
return canSeat(enemies,numTables,tables,friendsLeft);
}

5/5

Point Breakdown:
1: Base Case 1 Checks if done seating all friends
1: Base Case 2 Checks if not enough tables
1: Removes the next friend from consideration in future recursive calls
In the above solution, this corresponds to removing the friend from the Vector of
friends. Different solutions may account for this in different ways
4: Checks each table for enemies of the current friend
1: iterates over each friend in table
3: checks if users are enemies in enemiesList
4: If table is free of enemies, adds current friend to table, recurses, and returns true if
recursive call returns true
1: adds current friend to table
1: recurses with correct arguments
2: leaves tables unmodified for next iteration. This can take multiple forms the
solution above makes a copy of tables, another valid solution would be to add
currFriend to tables[i] then remove currFriend from tables[i] after the
recursive call returns.
4: Also tries adding current friend to a new table and recurses.
3: creates a new table, adds currFriend to it and adds the new table to tables
1: Updates numTables properly. For the solution above, the proper update was to do
nothing, but some solutions may require it it depends on how the implement Base Case

Anda mungkin juga menyukai