• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
CS106B
Handout #22S
Yves Lu
July 8, 2008
Section Solutions #3
Problem 1: List Mnemonics
/** Function: ListMnemonics
* Usage: ListMnemonics(str);
*--------------------------

* This function lists all of the mnemonics for the string of digits
* stored in the string str. The correspondence between digits and
* letters is the same as that on the standard telephone dial. The
* implementation at this level is a simple wrapper function that
* provides the arguments necessary for the recursive call.
*/
void ListMnemonics(string str){

RecursiveMnemonics("", str);
}/** Function: RecursiveMnemonics
* Usage: RecursiveMnemonics(prefix, rest);
*----------------------------------------

* This function does all of the real work for ListMnemonics and
* implements a more general problem with a recursive solution
* that is easier to see. The call to RecursiveMnemonics generates
* all mnemonics for the digits in the string rest prefixed by the
* mnemonic string in prefix. As the recursion proceeds, the rest
* string gets shorter and the prefix string gets longer.
*/
void RecursiveMnemonics(string prefix, string rest){
if (rest.length() == 0){

cout << prefix << endl;

}else{
string options = DigitLetters(rest[0]);
for (int i = 0; i < options.length(); i++)

RecursiveMnemonics(prefix + options[i], rest.substr(1));
}
}/** Function: DigitLetters
* Usage: digits = DigitLetters(ch);
*---------------------------------

* This function returns a string consisting of the legal
* substitutions for a given digit character. Note that 0 and
* 1 are handled just by leaving that digit in its position.
*/
string DigitLetters(char ch){

switch (ch) {
case '0': return ("0");
case '1': return ("1");
case '2': return ("ABC");

case '3': return ("DEF");
case '4': return ("GHI");
case '5': return ("JKL");
case '6': return ("MNO");
case '7': return ("PRS");
case '8': return ("TUV");
case '9': return ("WXY");
default: Error("Illegal digit");

}
}Problem 2: Shortest Path through a Maze
/** Function: ShortestPathLength
* Usage: shortestPath = ShortestPathLength(pt);
*-----------------------------------------

* This function looks for the length of the shortest path through the
* maze starting at the specified position pt. If none exists it
* returns NoSolution, otherwise it returns the shortest distance.
** To find the length of the shortest path, the function computes the

* length of the shortest path from all directions around the
* point. The shortest path is then one more than the smallest of
* these (since it takes one step to move in the given direction). The
* base cases occurs when you are outside the maze, in which case the
* shortest path has zero length, or if you have already been to a

* location, in which case NoSolution is returned since you\u2019ve already
* visited that location and thus don\u2019t need to visit it again.
*/
int ShortestPathLength(pointT pt){

int shortest, len;
if (OutsideMaze(pt))return 0;
if (IsMarked(pt))return NoSolution;
shortest = NoSolution;
MarkSquare(pt);

for (directionT dir = North; dir <= West; dir=directionT(dir+1)){

if (!WallExists(pt, dir)){
len = ShortestPathLength(AdjacentPoint(pt, dir));
if (len < shortest)

shortest = len;
}
}UnmarkSquare(pt);
if (shortest == NoSolution) return NoSolution;
else return (shortest + 1);
}
Problem 3: Edit Distance
/** Function: Min
* Usage: min = Min(a, b, c);
*-----------------------------------------

* Function returns the minimum value of a, b and c.
*/
int Min(int a, int b, int c) {

if (a < b){
if (a < c) return a;
else return c;

}else{
if (b < c)return b;
else return c;
}
}/** Function: EditDistance
* Usage: editDistance = EditDistance(s, t);
*-----------------------------------------

* This function computes the edit distance between the two
* passed in parameters. The edit distance is the minimum number of
* insertions, deletions or substitutions required to transform
* s in to t. To compute this we have two base cases.
* If the strings are equal, then we know the minimum distance is
* 0 since they are the same. If either string is empty, then we have
* an edit distance equal to the length of the other string\u2013 since it
* will take that many insertions to create the other string.
** Otherwise, if the two strings share the same first character, then

* the edit distance is just the edit distance between the result of
* the remaining characters. If they aren\u2019t equal, then the edit

* distance is just the minimum of the edit distance between the
* strings created by performing a single substitution, insertion or
* deletion to turn s in to t.
*/
int EditDistance(string s, string t)
{

if (s == t) return 0;
if (s == "") return t.length();
if (t == "") return s.length();
if (s[0] == t[0]){

// found match, recur on rest
return EditDistance(s.substr(1), t.substr(1));
}else {

return 1 + Min(EditDistance (s.substr(1), t.substr(1)),
EditDistance (s.substr(1), t),
EditDistance (s, t.substr(1)));

}
}
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...