25-CS106B-Midterm - CS106B Spring 2010 Handout 25 May 3rd,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS106B Spring 2010 Handout 25 May 3rd, 2010 CS106B Midterm Examination This is an open-note, open-reader exam. You can refer to any course handouts, textbooks, handwritten lecture notes, and printouts of any code relevant to any CS106B assignment. You may not use any laptops, cell phones, or handheld devices of any sort. Good luck! Section Leader: Last Name: First Name: _____________________ _____________________ _____________________ I accept the letter and spirit of the honor code. I’ve neither given nor received aid on this exam. I pledge to write more neatly than I ever have in my entire life. (signed) __________________________________________________________ Score Grader 1. HTML Entities 2. Longest Increasing Subsequences 3. Vampire Numbers Total [10] ______ ______ [10] ______ ______ [10] ______ ______ [30] ______ ______ 2 Problem 1: HTML Entitles [10 points] HTML is a language that helps decorate plain text with tags that serve as instructions on how to style the text within a web browser. Tags take the form of strings that begin with '<' and end with '>'. Some of the more common tags are <b> (for styling text as boldface), <u> (for underlining text), and <i> (for italicizing text). Because '<' and '>' are so prevalent in HTML, it’s difficult to include actual < and > characters in plain text because the browser might confuse them as markup. As a result, the following plain text: < and > are the most popular relational operators in C++. might give the impression that there’s a leading HTML and tag. You could argue that browsers should just know that and isn’t a genuine tag, but as it turns out, HTML documents can define their own tags, and in principle <and> might be a legitimate tag type within a particular document. The solution is to replace the plain text <’s and the >’s with HTML entities—strings used to represent characters in a document that otherwise have special meaning. In standard HTML, < is represented by "&lt;" (without the double quotes), and > is represented by "&gt;". And because & has special meaning now, there’s also an HTML entity for it: "&amp;". In fact, there are a whole slew of HTML entities beyond these three. Here are just a few examples: < > & " ' ¢ &lt; &gt; &amp; &quot; &apos; &cent; Every single HTML entity begins with & and ends with ;, and is associated with some single character in (what we’re simplifying it to be) the ASCII character set. To be clear: John said, &quot;Hi!&quot; is really John said, "Hi!" Cold Stone&apos;s sundaes: 49&cent;! is really Cold Stone's sundaes: 49¢! A&amp;P Supermarket is really A&P Supermarket x &lt; 7 &amp;&amp; y &gt;= 10 is really x < 7 && y >= 10 You’re to write a function called RestorePlainText, which takes a string and a map of HTML entity strings to characters, and returns a copy of the incoming string, save that all of its HTML entities have been replaced with the characters they represent. The Map<char> is keyed on the 3 HTML entity (without the leading & and trailing ; so that strings like "lt" and "amp" are keys, not "&lt;" and "&amp;") and each maps to the character it represents (i.e. '<' or '&'). If the string contains what appears to be an HTML entity not in the Map<char>, then that entity is left in the string as is. You can assume the string is otherwise well formed, in that every opening & is eventually balanced by a closing ;, and that there aren’t any unpaired ampersands or semicolons. Place your implementation of the following this and the following page. string RestoreString(string str, Map<char>& entityMap) { 4 5 Problem 2: Longest Increasing Subsequence [10 points] Given a string of lowercase letters, write a function LongestIncreasingSubsequence that returns the longest increasing subsequence. Recall that a subsequence of a string is the same string, except that an arbitrary number of its characters have been removed (and the order of the characters that remain is preserved.) By increasing, we mean that as a character sequence the string is sorted from low to high. Given the string "aiemckgobjfndlhp", each of the following is a subsequence: "mjp" "acgjlp" "aiemckgobjfndlhp" "aemckobjfnlp" "emjfnp" "aegjnp" Of the six subsequences above, only "acgjlp" and "aegjnp" are increasing, since as character arrays they are sorted from low to high. And because there aren’t any increasing subsequences of length 7 or more (an exhaustive search demonstrates this—just trust us), "acgjlp" and "aegjnp" are each longest increasing subsequences. Using this and the next page, write a recursive function that takes a string and returns the longest increasing subsequence. If there are two or more such subsequences, your function can return any one of them. string LongestIncreasingSubsequence(string str) { 6 7 Problem 3: Vampire Numbers [10 points] When two positive numbers m and n have the same number of digits and their product p is a numerical permutation of the combined digits of m and n, we call p a Vampire number (and m and n are its fangs). For instance, 125460 is a vampire number, because it can be written as 204 times 615. 16758243290880 is also a Vampire number, because it just happens to equal 1982736 times 8452080. Note that the digits present in 1982736 and 8452080 are also present—with the same frequencies—in 16758243290880. Using recursive backtracking, implement the isVampireNumber predicate function, which has the following prototype: bool isVampireNumber(int number, int& first, int& second); When the provided number is a vampire number, isVampireNumber should return true and place the two factors in the spaces referenced by first and second. When the provided number is not a vampire number, isVampireNumber should return false without concern for what first and second end up referencing. Use this and the next page for your solution. Hint: Write a wrapper function, and use StringToInteger and IntegerToString to convert back and forth between a number and its string form, which allows you to access arbitrary digits more easily. bool isVampireNumber(int number, int& first, int& second) { 8 ...
View Full Document

This note was uploaded on 07/20/2010 for the course CS 443 taught by Professor Jayana during the Spring '10 term at École Normale Supérieure.

Ask a homework question - tutors are online