H26 PracticeSoln PDF
H26 PracticeSoln PDF
H26 PracticeSoln PDF
We'd love to see everyone Tuesday evening, but if that's impossible for you, e-mail head
TA Jason by noon Thursday to arrange an alternate Tuesday
time. Remote SCPD students should email Jason by Thursday to initiate arrangements
for taking the exam onsite.
Version B does find-replace operations on the original string to produce the result.
string ConvertMacLineEndingsToPC(string str)
{
int pos = 0;
while ((pos = str.find("\n", pos)) != string::npos) {
str.replace(pos, 1, "\r\n"); // or str.insert(pos, "\r");
pos += 2;
}
return str;
}
4)
bool CanSpell(string word, Vector<string> & cubes)
{
if (word == "") return true;
6) There are many correct variations, here is one recursive and one iterative solution.
8a) Winky is O(2N). The recurrence is T(n) = T(n-1) + T(n-2) + T(n-3) + … + T(2) + T(1)
+ T(0). Repeatedly substituting to expand and grouping terms will allow you to see the
doubling pattern that arises. Another strategy is to notice that T(n-2) + T(n-3) + …+ T(1)
+ T(0) is T(n –1) and reverse-substitute to get T(n) = 2T(n-1) which is the Towers of
Hanoi recurrence that solves to O(2N).
8b) As is, the code prints "cucumber" because the salad stack is unaffected by the call. If
the stack is instead passed by reference, the code will print "lettuce" because the Toss
function reversed the stack contents.
8c) Selection Sort. It does many comparisons to determine the remaining max, but does
at most N-1 swaps total, in contrast to insertion sort, which shuffles each student down
the hall into the correct spot, on average requiring N/2 moves per student or N 2 /2 total
moves.