diff --git a/Figures/bigo.jpg b/Figures/bigo.jpg new file mode 100644 index 00000000..54659009 Binary files /dev/null and b/Figures/bigo.jpg differ diff --git a/Figures/bigo1.jpg b/Figures/bigo1.jpg new file mode 100644 index 00000000..1ebf1508 Binary files /dev/null and b/Figures/bigo1.jpg differ diff --git a/Figures/count.jpg..jpeg b/Figures/count.jpg..jpeg new file mode 100644 index 00000000..703f634e Binary files /dev/null and b/Figures/count.jpg..jpeg differ diff --git a/_sources/AlgorithmAnalysis/Exercises.rst b/_sources/AlgorithmAnalysis/Exercises.rst new file mode 100644 index 00000000..eb95e048 --- /dev/null +++ b/_sources/AlgorithmAnalysis/Exercises.rst @@ -0,0 +1,475 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + +.. mchoice:: cds_AA_count + :author: Cynthia Lee + + Is that right to count how many times we perform each line of code when n is the size of vector? + + .. figure:: Figures/count.jpg + :width: 200px + :align: center + :alt: count + :figclass: align-center + + - Yes + + + Correct! Then we will find we really don't care about the +5 or the 3 for that matter. + + - No + + - Wrong! + +.. mchoice:: cds_AA_bigo1 + :author: Cynthia Lee + + f2 is O(f1)? + + .. figure:: Figures/bigo-1.jpg + :width: 200px + :align: center + :alt: Bigo + :figclass: align-center + + - Yes + + + Correct! f1 is above f2—an “upper bound” f2 <= O(f1). + + - No + + - Wrong! + +.. mchoice:: cds_AA_bigo2 + :author: Cynthia Lee + + f1 is O(f2)? + + .. figure:: Figures/bigo-1.jpg + :width: 200px + :align: center + :alt: Bigo + :figclass: align-center + + - Yes + + + Correct! f(n) = O(g(n)), if there are positive constants c and n0 such that f(n) ≤ c * g(n) for all n ≥ n0. We can move f2 above f1 by multiplying by c + + - No + + - Wrong! + + .. mchoice:: cds_aa_bigo3 + :author: Cynthia Lee + + f3 is O(f1)? + + .. figure:: Figures/bigo-2.jpg + :width: 200px + :align: center + :alt: Bigo + :figclass: align-center + + - Yes + + - Wrong! + + - No + + + Correct! f3 <= O(f1) + +.. mchoice:: cds_AA_bigo4 + :author: Cynthia Lee + + f1 is O(f3)? + + .. figure:: Figures/bigo-2.jpg + :width: 200px + :align: center + :alt: Bigo + :figclass: align-center + + - Yes + + + Correct! + + - No + + - Wrong! + +.. mchoice:: cds_AA_fomular1 + :author: Cynthia Lee + + Let f(n) = 3 log2 n + 4 n log2 n + n. Which of the following is true? + + + - f(n) = O(log2n) + + - Wrong! + + - f(n) = O(nlog2n) + + - Wrong! + + - f(n) = O(n^2) + + - Wrong! + + - f(n) = O(n) + + - Wrong! + + - Other/none/more + + + Correct! + +.. mchoice:: cds_AA_fomular2 + :author: Cynthia Lee + + Let f(n) = 546 + 34n + 2n^2. Which of the following is true? + + + - f(n) = O(2^n) + + - Wrong! + + - f(n) = O(n^2) + + + Correct! + + - f(n) = O(n) + + - Wrong! + + - f(n) = O(n^3) + + - Wrong! + + - Other/none/more + + - Wrong! + +.. mchoice:: cds_AA_fomular3 + :author: Cynthia Lee + + Let f(n) = 2^n + 14n^2 + 4n^3. Which of the following is true? + + + - f(n) = O(2^n) + + + Correct! + + - f(n) = O(n^2) + + - Wrong! + + - f(n) = O(n) + + - Wrong! + + - f(n) = O(n^3) + + - Wrong! + + - Other/none/more + + - Wrong! + +.. mchoice:: cds_AA_fomular4 + :author: Cynthia Lee + + Let f(n) = 100. Which of the following is true? + + + - f(n) = O(2^n) + + - Wrong! + + - f(n) = O(n^2) + + - Wrong! + + - f(n) = O(n) + + - Wrong! + + - f(n) = O(n^100) + + - Wrong! + + - Other/none/more + + + Correct! O(1) can work. + +.. mchoice:: cds_AA_memory + :author: Cynthia Lee + + Each memory address indexes one byte (8 bits). Can you deduce from the drawing at right how many bits are used to represent int and double, respectively? + + .. figure:: Figures/diagram.jpg + :width: 200px + :align: center + :alt: diagram + :figclass: align-center + + - 4bits, 8bits + + - Wrong! + + - 32bits, 64bits + + + Correct! + + - 16bits, 16bits + + - Wrong! + + - 16bits, 32bits + + - Wrong! + +.. mchoice:: cds_AA_pointer + :author: Cynthia Lee + + What prints here? + + :: + + int a[4] = {91, -2, 85, 17}; + int* p = a; + p[1] = 5; + p++; + cout << *p << endl; + *(p + 2) = 26; + cout << p[2] << endl; + cout << a[2] << endl; + + - 26, 26 + + - Wrong! + + - 26, [other] + + + Correct! [other] is 85. p = &a[0]; p = &a[1]; a[3] = 26; + + - [other], 26 + + - Wrong! + + - [other], [other] + + - Wrong! + +.. mchoice:: cds_AA_memory_allocation + :author: Cynthia Lee + + What prints here? + + :: + + int * p1 = new int;//0x12 + *p1 = 5; + int * p2 = new int;//0x4 + *p2 = 7; + int * p3 = new int;//0x20 + *p3 = 8675309; // important phone # + *p1 = *p2; + cout << p1 << “ “ << *p1 << endl; + + - 0x12, 5 + + - Wrong! + + - 0x4, 7 + + - Wrong! + + - 0x12, 7 + + + Correct! + + - 0x4, 5 + + - Wrong! + +.. mchoice:: cds_AA_memory_allocation1 + :author: Cynthia Lee + + What prints here? + + :: + + int * p1 = new int;//0x12 + *p1 = 5; + int * p2 = new int;//0x4 + *p2 = 7; + int * p3 = new int;//0x20 + *p3 = 8675309; // important phone # + *p1 = *p2; + cout << p1 << “ “ << *p1 << endl; + p1 = p2; + cout << p1 << “ “ << *p1 << endl; + + - 0x12, 5 + + - Wrong! + + - 0x4, 7 + + + Correct! + + - 0x12, 7 + + - Wrong! + + - 0x4, 5 + + - Wrong! + +.. mchoice:: cds_AA_memory_allocation2 + :author: Cynthia Lee + + These last three lines… + + :: + + int * p1 = new int;//0x12 + *p1 = 5; + int * p2 = new int;//0x4 + *p2 = 7; + int * p3 = new int;//0x20 + *p3 = 8675309; // important phone # + *p1 = *p2; + cout << p1 << “ “ << *p1 << endl; + p1 = p2; + cout << p1 << “ “ << *p1 << endl; + delete p1; + delete p2; + cout << *p3 << endl; //print important phone # + + - Looks good! + + - Wrong! + + - Didn’t do enough deleting + + - Wrong! + + - Did too much deleting + + + Correct! In line 9, we set p1 to be pointing at p2's address. Then in line 11, we "delete p1", which will delete the object that p1 is pointing to (which is also the object that p2 is pointing to). When we do "delete p2" again, then we will get an error since we are trying to delete free memory. + + + - Accessed memory after deleting + + - Wrong! + +.. mchoice:: cds_AA_set1 + :author: Cynthia Lee + + Which are Cliques? + + .. figure:: Figures/set.jpg + :width: 200px + :align: center + :alt: set + :figclass: align-center + + + - { B, D, E, F } + + - Wrong! + + - { B, C, D } + + - Wrong! + + - { B, C } + + - Wrong! + + - Other/none/more than one + + + Correct! A & C + + .. mchoice:: cds_AA_set2 + :author: Cynthia Lee + + Which are Independent Sets? + + .. figure:: Figures/set.jpg + :width: 200px + :align: center + :alt: set + :figclass: align-center + + + - { A, C, G } + + - Wrong! + + - { A, C, F } + + - Wrong! + + - { A, E } + + - Wrong! + + - Other/none/more than one + + + Correct! A & B & C + + +.. mchoice:: cds_AA_vertex + :author: Cynthia Lee + + Find a vertex cover S that uses the fewest number of vertices (|S| is minimized). What is |S|? + + + - 1 + + - Wrong! + + - 2 + + - Wrong! + + - 3 + + + Correct! + + - 4 + + - Wrong! + + - >4 + + - Wrong! + + .. mchoice:: cds_AA_possibleTrue + :author: Cynthia Lee + + how many of the following are possibly true? + if (!x0 && x0) + if (!x0 && (x1 || !x2) && (x2 || x3)) + if ((!x0 || !x1) && (x0 || x2) && x1 && !x2) + + + - 0 + + - Wrong! + + - 1 + + + Correct! + + - 2 + + - Wrong! + + - 3 + + - Wrong! diff --git a/_sources/AlgorithmAnalysis/Figures/bigo-1.jpg b/_sources/AlgorithmAnalysis/Figures/bigo-1.jpg new file mode 100644 index 00000000..4095e7f9 Binary files /dev/null and b/_sources/AlgorithmAnalysis/Figures/bigo-1.jpg differ diff --git a/_sources/AlgorithmAnalysis/Figures/bigo-2.jpg b/_sources/AlgorithmAnalysis/Figures/bigo-2.jpg new file mode 100644 index 00000000..43ea984a Binary files /dev/null and b/_sources/AlgorithmAnalysis/Figures/bigo-2.jpg differ diff --git a/_sources/AlgorithmAnalysis/Figures/count.jpg b/_sources/AlgorithmAnalysis/Figures/count.jpg new file mode 100644 index 00000000..e1385cde Binary files /dev/null and b/_sources/AlgorithmAnalysis/Figures/count.jpg differ diff --git a/_sources/AlgorithmAnalysis/Figures/diagram.jpg b/_sources/AlgorithmAnalysis/Figures/diagram.jpg new file mode 100644 index 00000000..febd38d4 Binary files /dev/null and b/_sources/AlgorithmAnalysis/Figures/diagram.jpg differ diff --git a/_sources/AlgorithmAnalysis/Figures/set.jpg b/_sources/AlgorithmAnalysis/Figures/set.jpg new file mode 100644 index 00000000..7ca6eeab Binary files /dev/null and b/_sources/AlgorithmAnalysis/Figures/set.jpg differ diff --git a/_sources/AlgorithmAnalysis/toctree.rst b/_sources/AlgorithmAnalysis/toctree.rst index fcfb1c2c..4c13fb7c 100644 --- a/_sources/AlgorithmAnalysis/toctree.rst +++ b/_sources/AlgorithmAnalysis/toctree.rst @@ -16,3 +16,4 @@ Analysis DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst + Exercises.rst diff --git a/_sources/Graphs/Exercises.rst b/_sources/Graphs/Exercises.rst new file mode 100644 index 00000000..c5604ad3 --- /dev/null +++ b/_sources/Graphs/Exercises.rst @@ -0,0 +1,187 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + +.. mchoice:: cds_graph_ways + :author: Cynthia Lee + + How many of the following are true? + + + - Adjacency list can be used for directed graphs + + - Wrong! + + - Adjacency list can be used for undirected graphs + + + Correct! + + - Adjacency matrix can be used for directed graphs + + - Wrong! + + - Adjacency matrix can be used for undirected graphs + + - Wrong! + + + + .. mchoice:: cds_graph_Eulerian + :author: Cynthia Lee + + Is this graph Eulerian? + + .. figure:: Figures/Eulerian1.jpg + :width: 200px + :align: center + :alt: Harsh_Eulerian + :figclass: align-center + + - yes + + - Wrong! + + - no + + + Correct! + + .. mchoice:: cds_graph_Eulerian2 + :author: Cynthia Lee + + Is this graph Eulerian? + + .. figure:: Figures/Eulerian2.jpg + :width: 200px + :align: center + :alt: Harsh_Eulerian + :figclass: align-center + + - yes + + + Correct! + + - no + - Wrong! + +.. mchoice:: cds_graph_search + :author: Cynthia Lee + + You predict the next step! + + .. figure:: Figures/search.jpg + :width: 200px + :align: center + :alt: Harsh_Eulerian + :figclass: align-center + + - K’s neighbors F,G,H are yellow and enqueued and their pare + + + Correct! + + - K’s neighbors G,H are yellow and enqueued and their parents are pointing to K + + - Wrong! + +.. mchoice:: cds_graph_eff + :author: Cynthia Lee + + You calculated the shortest path for yourself (Yosemite to Palo Alto) and let’s just say that it took time X = O((|E| + |V|)log|V|) + How long will it take you, in total, to calculate the shortest path for ALL of your relatives? + + - O(|V|*X) + + + Correct! + + - O(|E|*|V|* X) + + - Wrong! + +.. mchoice:: cds_graph_spanning + :author: Cynthia Lee + + How many distinct spanning trees are in this graph? + + .. figure:: Figures/spanning.jpg + :width: 200px + :align: center + :alt: graph-trees + :figclass: align-center + + - 0-1 + + - Wrong! + + - 2-3 + + - Wrong! + + - 4-5 + + - Wrong! + + - 6-7 + + - Wrong! + + - > 7 + + + Correct! + +.. mchoice:: cds_graph_MST + :author: Cynthia Lee + + How many distinct MSTs are in this graph? + + .. figure:: Figures/MST.jpg + :width: 200px + :align: center + :alt: graph-trees + :figclass: align-center + + - 0-1 + + - Wrong! + + - 2-3 + + - Wrong! + + - 4-5 + + + Correct! + + - 6-7 + + - Wrong! + + - > 7 + + - Wrong! + +.. mchoice:: cds_graph_tf + :author: Cynthia Lee + + Having at least two edges with the same weight is SUFFICIENT for having more than one distinct minimum spanning tree. + + - TRUE + + - Wrong! + + - FALSE + + + Correct! i.e., “If at least two edges have the same weight, then there is more than one MST” + +.. mchoice:: cds_graph_tf2 + + Having at least two edges with the same weight is NECESSARY for having more than one distinct minimum spanning tree. + + - TRUE + + + Correct! i.e., “If there is more than one MST, then at least two edges have the same weight.” + + - FALSE + + - Wrong! diff --git a/_sources/Graphs/Figures/Eulerian1.jpg b/_sources/Graphs/Figures/Eulerian1.jpg new file mode 100644 index 00000000..9a01861b Binary files /dev/null and b/_sources/Graphs/Figures/Eulerian1.jpg differ diff --git a/_sources/Graphs/Figures/Eulerian2.jpg b/_sources/Graphs/Figures/Eulerian2.jpg new file mode 100644 index 00000000..363e0fea Binary files /dev/null and b/_sources/Graphs/Figures/Eulerian2.jpg differ diff --git a/_sources/Graphs/Figures/MST.jpg b/_sources/Graphs/Figures/MST.jpg new file mode 100644 index 00000000..ea66bd35 Binary files /dev/null and b/_sources/Graphs/Figures/MST.jpg differ diff --git a/_sources/Graphs/Figures/search.jpg b/_sources/Graphs/Figures/search.jpg new file mode 100644 index 00000000..43238edf Binary files /dev/null and b/_sources/Graphs/Figures/search.jpg differ diff --git a/_sources/Graphs/Figures/spanning.jpg b/_sources/Graphs/Figures/spanning.jpg new file mode 100644 index 00000000..e8857ae5 Binary files /dev/null and b/_sources/Graphs/Figures/spanning.jpg differ diff --git a/_sources/Graphs/toctree.rst b/_sources/Graphs/toctree.rst index 6a62f077..cc8d8a89 100644 --- a/_sources/Graphs/toctree.rst +++ b/_sources/Graphs/toctree.rst @@ -31,3 +31,4 @@ Graphs and Graph Algorithms DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst + Exercises.rst diff --git a/_sources/Introduction/Exercises.rst b/_sources/Introduction/Exercises.rst new file mode 100644 index 00000000..3fdacf97 --- /dev/null +++ b/_sources/Introduction/Exercises.rst @@ -0,0 +1,394 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + +.. mchoice:: cds_intro_bug + :author: Cynthia Lee + + Can you find the bug here? + + :: + + // (includes omitted) + static void clearBoard(Grid board); + + int main(){ + Grid board(8,8); + /* note that clearing here is not + * strictly necessary */ + clearBoard(board); + // …more code to come… + return 0; + } + + static void clearBoard(Grid board){ + for (int i=0; i& board) + + - The code is inefficient. + + - Wrong! + + + + +.. mchoice:: cds_intro_betterone + :author: Cynthia Lee + + Which one is better? + + :: + + // The first one + static void printBoard(Grid& board){ + for (int i=0; i board){ + for (int i=0; i> mymap; + + Vector numbers; + numbers.add(1); + numbers.add(2); + numbers.add(3); + + mymap["123"] = numbers; + + mymap["123"].add(4); 
 + cout << “New size: " << mymap["123"].size() << endl; + + - 3 + + - Wrong! Take notice of it, it is returning a reference + + - 4 + + + Correct! Returning a reference + + - Error + + - Wrong! + + - Other + + - Wrong! + + +.. mchoice:: cds_intro_reference2 + :author: Cynthia Lee + + What is the outcome of code? + + :: + + Map> mymap; + + Vector numbers; + numbers.add(1); + numbers.add(2); + numbers.add(3); + + mymap["123"] = numbers; + + Vector plainTest = mymap["123"]; + plainTest.add(4); + + cout << “New size: " << mymap["123"].size() << endl; + + - 3 + + + Correct! + + - 4 + + - Wrong! The outcome is about mymap but not plainTest. + + - Error + + - Wrong! + + - Other + + - Wrong! + + +.. mchoice:: cds_intro_reference3 + :author: Cynthia Lee + + What is the outcome of code? + + :: + + Map> mymap; + + Vector numbers; + numbers.add(1); + numbers.add(2); + numbers.add(3); + + mymap["123"] = numbers; + + Vector plainTest = mymap["123"]; + referenceTest.add(4); + + cout << “New size: " << mymap["123"].size() << endl; + + - 3 + + - Wrong! + + - 4 + + + Correct! + + - Error + + - Wrong! + + - Other + + - Wrong! + +.. mchoice:: cds_intro_pointer + :author: Cynthia Lee + + What is the correct picture? + + :: + + head->next->next = new ListNode; + + head->next->next->data = 40; + + .. figure:: Figures/pointer-before.jpg + :width: 200px + :align: pointer-before + :figclass: align-center + + - .. figure:: Figures/pointer-after1.jpg + :width: 200px + :align: pointer-before + :figclass: align-center + + - Wrong! + + - .. figure:: Figures/pointer-after2.jpg + :width: 200px + :align: pointer-before + :figclass: align-center + + + Correct! + + - Using “next” that is NULL gives an error + + - Wrong! + +.. mchoice:: cds_intro_reference1 + :author: Cynthia Lee + + What is the outcome of code? + + :: + + Map> mymap; + + Vector numbers; + numbers.add(1); + numbers.add(2); + numbers.add(3); + + mymap["123"] = numbers; + + mymap["123"].add(4); 
 + cout << “New size: " << mymap["123"].size() << endl; + + - 3 + + - Wrong! Take notice of it, it is returning a reference + + - 4 + + + Correct! Returning a reference + + - Error + + - Wrong! + + - Other + + - Wrong! + +.. mchoice:: cds_intro_inherMammel + :author: Cynthia Lee + + What is printed? + Siamese * s = new Siamese; + cout << s->toString(); + + :: + + class Mammal { + public: + virtual void makeSound() = 0;
 string toString() { return “Mammal”; }
 }; + class Cat : public Mammal { 
 public: 
 virtual void makeSound() { cout << “rawr” << endl; } + string toString() { return “Cat”; }
 };
 class Siamese : public Cat { + public: + virtual void makeSound() { cout << “meow” << endl; } + string toString() { return “Siamese”; }
 }; + + - “Mammal” + + - Wrong! + + - “Cat” + + - Wrong! + + - “Siamese” + + + Correct! + + - Gives an error + + - Wrong! + +.. mchoice:: cds_intro_inherMammel2 + :author: Cynthia Lee + + What is printed? + Cat * c = new Siamese; + cout << c->toString(); + + :: + + class Mammal { + public: + virtual void makeSound() = 0;
 string toString() { return “Mammal”; }
 }; + class Cat : public Mammal { 
 public: 
 virtual void makeSound() { cout << “rawr” << endl; } + string toString() { return “Cat”; }
 };
 class Siamese : public Cat { + public: + virtual void makeSound() { cout << “meow” << endl; } + string toString() { return “Siamese”; }
 }; + + - “Mammal” + + - Wrong! + + - “Cat” + + + Correct! + + - “Siamese” + + - Wrong! + + - Gives an error + + - Wrong! + +.. mchoice:: cds_intro_inherMammel3 + :author: Cynthia Lee + + What is printed? + Cat * c = new Siamese; + c->makeSound(); + + :: + + class Mammal { + public: + virtual void makeSound() = 0;
 string toString() { return “Mammal”; }
 }; + class Cat : public Mammal { 
 public: 
 virtual void makeSound() { cout << “rawr” << endl; } + string toString() { return “Cat”; }
 };
 class Siamese : public Cat { + public: + virtual void makeSound() { cout << “meow” << endl; } + string toString() { return “Siamese”; }
 }; + + - “rawr” + + - Wrong! + + - “meow” + + + Correct! + + - “Siamese” + + - Wrong! + + - Gives an error + + - Wrong! + + .. mchoice:: cds_intro_design + :author: Cynthia Lee + + Which best explains good design of destructors in polymorphic classes? + + + - Destructors are specific to each class, so we don’t need to apply virtual to them + + - Wrong! + + - Destructors should be virtual or we will cause a memory leak in cases like this: “DerivedType obj = new DerivedType(); delete obj;” + + - Wrong! + + - Destructors should be virtual or we will cause a memory leak in cases like this: “BaseType obj = new DerivedType(); delete obj;” + + + Correct! C++ style rule always make destructor virtual. + + - Both B and C + + - Wrong! \ No newline at end of file diff --git a/_sources/Introduction/Figures/pointer- after1.jpg b/_sources/Introduction/Figures/pointer- after1.jpg new file mode 100644 index 00000000..a6b4ac6b Binary files /dev/null and b/_sources/Introduction/Figures/pointer- after1.jpg differ diff --git a/_sources/Introduction/Figures/pointer- after2.jpg b/_sources/Introduction/Figures/pointer- after2.jpg new file mode 100644 index 00000000..d5b2085b Binary files /dev/null and b/_sources/Introduction/Figures/pointer- after2.jpg differ diff --git a/_sources/Introduction/Figures/pointer-before.jpg b/_sources/Introduction/Figures/pointer-before.jpg new file mode 100644 index 00000000..085c38d6 Binary files /dev/null and b/_sources/Introduction/Figures/pointer-before.jpg differ diff --git a/_sources/Introduction/toctree.rst b/_sources/Introduction/toctree.rst index ff29ae39..4b9cadfd 100644 --- a/_sources/Introduction/toctree.rst +++ b/_sources/Introduction/toctree.rst @@ -18,6 +18,7 @@ Introduction ObjectOrientedProgrammingDerivedClasses.rst Graphics.rst Summary.rst + Exercises.rst DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst diff --git a/_sources/LinearBasic/Exercises.rst b/_sources/LinearBasic/Exercises.rst new file mode 100644 index 00000000..c4b14eb4 --- /dev/null +++ b/_sources/LinearBasic/Exercises.rst @@ -0,0 +1,178 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + +.. mchoice:: cds_LB_input + :author: Cynthia Lee + + What does this code do? + + :: + + void mystery(ifstream& infile) { + Stack lines; + string line; + while (getline(infile,line)) { + lines.push(line); + } + infile.close(); + while (!lines.isEmpty()) { + cout << lines.pop() + << endl; + } + } + + - Prints all lines of a file to cout + + - Wrong! Stacks are a type of container adaptors that follow LIFO(Last In First Out). + + - Prints only the first line of a file to cout + + - Wrong! Every line != empty should be printed. + + - Prints only the last line of a file to cout + + - Wrong! Every line != empty should be printed. + + - Prints all lines of a file to cout in reverse + + + Correct! + + - All/ none/ more than one of the above + + - Wrong! + + +.. mchoice:: cds_LB_diffinput + :author: Cynthia Lee + + Ignoring operator precedence rules, how many distinct results are there to the following arithmetic expression : 3 * 3 + 3 * 3 ? + + - 1 + + - Wrong! The computer won't follow the math rules. + + - 2 + + - Could you come up with a little bit more? + + - 3 + + + Correct! (3 * 3) + (3 * 3) ; 3 * (3 + 3) * 3 ; 3 * (3 + (3 * 3)) + + - 4 + + - Wrong! Check out if there are the same ones for your results. + + - More than 4. + + - Wrong! Check out if there are the same ones for your results. + +.. mchoice:: cds_LB_polishnotation + :author: Cynthia Lee + + Which infix expression does this postfix expression: 4 3 * 7 2 5 * + + equivalent to ? + + + + - ((4*3) + (7*2)) + 5 + + - Wrong! + + - (4*3) + ((7+2) + 5) + + - Wrong! + + - (4*3) + (7 + (2*5)) + + + Correct! + + - Other/none/more than one + + - Wrong! + + +.. mchoice:: cds_LB_polishnotationcont + :author: Cynthia Lee + + Contents of the stack of the last question, reading from top-down: + + + - 7, 12 + + - Wrong! You might miss the "*". + + - 2, 7, 12 + + - Wrong! 5 seems to be the last number + + - 10, 7, 12 + + + Correct! + + - 10, 5, 2, 7, 12 + + - Wrong! 5 has been operated. + + +.. mchoice:: cds_LB_fifo + :author: Cynthia Lee + + How many of these operate as FIFO/Queue? + + + - Patients waiting in a hospital Emergency Room + + + Correct! + + - Passengers boarding and exiting an airplane + + - It should be last in, first out, or first in first out if you take first class into account. So actually we cannot be sure. + + - Passengers boarding and exiting an elevator (assume all board on the ground floor and exit at the same destination) + + - It should be last in, first out. + + - People waiting in line for an amusement park ride + + + Correct! + +.. mchoice:: cds_LB_Josephus + :author: Cynthia Lee + +.. figure:: Figures/Josephus.jpg + :width: 200px + :align: center + :alt: Josephus survivor puzzle + :figclass: align-center + + People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed. + + Where do you sit? + + + - 1 or 2 + + + Correct! + + - 1 or 6 + + - Wrong. + + - 5 or 10 + + - Wrong. + + - 5 or 6 + + - Wrong. + + + + + + + diff --git a/_sources/LinearBasic/Figures/Josephus.jpg b/_sources/LinearBasic/Figures/Josephus.jpg new file mode 100644 index 00000000..bce5919b Binary files /dev/null and b/_sources/LinearBasic/Figures/Josephus.jpg differ diff --git a/_sources/LinearBasic/Figures/basicqueue.png b/_sources/LinearBasic/Figures/basicqueue.png new file mode 100644 index 00000000..55d5ca82 Binary files /dev/null and b/_sources/LinearBasic/Figures/basicqueue.png differ diff --git a/_sources/LinearBasic/toctree.rst b/_sources/LinearBasic/toctree.rst index 446865d2..30173a00 100644 --- a/_sources/LinearBasic/toctree.rst +++ b/_sources/LinearBasic/toctree.rst @@ -27,3 +27,4 @@ Linear Structures DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst + Exercises.rst diff --git a/_sources/Recursion/Exercises.rst b/_sources/Recursion/Exercises.rst new file mode 100644 index 00000000..22c713e8 --- /dev/null +++ b/_sources/Recursion/Exercises.rst @@ -0,0 +1,447 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + +.. mchoice:: cds_Recursive_secondout + :author: Cynthia Lee + + What is the second thing printed when we call factorial(10)? + + :: + + long factorial (int n) { + cout << n << endl; + long result; + if (n == 1) result = 1; + else result = n * factorial(n – 1); + return result; +} + - 1 + + - Wrong! + + - 2 + + + Correct! 2 * 1 + + - 10 + + - Wrong! + + - 90 + + - Wrong! + + +.. mchoice:: cds_Recursive_third + :author: Cynthia Lee + + What is the third thing printed when we call factorial(10)? + + :: + + long factorial (int n) { + cout << n << endl; + long result; + if (n == 1) result = 1; + else result = n * factorial(n – 1); + return result; +} + + - 2 + + - Wrong! + + - 3 + + - Wrong! It starts from 10 to 1 not from 1 to 10. + + - 8 + + + Correct! + + - 6 + + - Wrong! returned or printed? + +.. mchoice:: cds_Recursive_fourth + :author: Cynthia Lee + + What is the fourth thing printed when we call factorial(10)? + + :: + + long factorial (int n) { + cout << n << endl; + long result; + if (n == 1) result = 1; + else result = n * factorial(n – 1); + return result; +} + + - 4 + + - Wrong! returned or printed? + + - 8 + + - Wrong! + + - 24 + + + Correct! 2 * 1 -> 3 * 2 * 1 -> 4 * 3 * 2 * 1; + + - 90 + + - Wrong! + +.. mchoice:: cds_Recursive_memory + :author: Cynthia Lee + + How does this look in memory? + + :: + + long factorial ( int n ) { + cout << n << endl; + long result; + if (n==1) result = 1; + else result = n*factorial(n–1); + return result; + } + + void myfunction(){ + int x = 10; + long xfac = 0; + xfac = factorial(x); + } + + //!REMINDER! + + .. mchoice:: cds_Recursive_fourth + :author: Cynthia Lee + + What is the fourth thing printed when we call factorial(10)? + + - Only three items remain: save yourself an unnecessary function call that would trivially divide them into halves of size 1, and just check all three. + + - Wrong! + + - Only two items remain: can’t divide into two halves with a middle, so just check the two. + + - Wrong! + + - Only one item remains: just check it. + + + Correct! Arm's length recursion writes more code than necessary by having special cases rather than letting recursions do the work. + + - No items remain: obviously we didn’t find it. + + - Wrong! + +.. mchoice:: cds_Recursive_binary + :author: Cynthia Lee + + Which line should be coded here? + + :: + + bool binarySearch(Vector& data, int key){ + return binarySearch(data, key, 0, data.size()-1); + } + + bool binarySearch(Vector& data, int key, int start, int end){ + // Write code to handle the base case “No items remain: + // obviously we didn’t find it by returning false + //to be continued… + } + + - if (data.size() <= 0) return false; + + - Wrong! + + - if (end < start) return false; + + + Correct! + + - if (end == start) return false; + + - Wrong! + + .. mchoice:: cds_Recursive_snow + :author: Cynthia Lee + + Where should this line of code be inserted to produce the pattern shown? + drawFilledBox(window, cx, cy, dim, "Gray", "Black"); + + .. figure:: Figures/snowflake.jpg + :width: 200px + :align: center + :alt: Boxy Snowflake + :figclass: align-center + + :: + static const double SCALE = 0.45; + + static void drawFractal(GWindow& window, double cx, double cy, double dim, int order) { + + if (order >= 0) { + + drawFractal(window, cx-dim/2, cy+dim/2, SCALE*dim, order-1); + + drawFractal(window, cx+dim/2, cy-dim/2, SCALE*dim, order-1); + + drawFractal(window, cx-dim/2, cy-dim/2, SCALE*dim, order-1); + + drawFractal(window, cx+dim/2, cy+dim/2, SCALE*dim, order-1); + + } + + } + + - drawFractal(window, cx-dim/2, cy+dim/2, SCALE*dim, order-1); + + - Wrong! + + - drawFractal(window, cx+dim/2, cy-dim/2, SCALE*dim, order-1); + + - Wrong! + + - drawFractal(window, cx-dim/2, cy-dim/2, SCALE*dim, order-1); + + - Wrong! + + - drawFractal(window, cx+dim/2, cy+dim/2, SCALE*dim, order-1); + + - Wrong! +' + - None of the above + + + Correct! + +.. mchoice:: cds_Recursive_realsnow + :author: Cynthia Lee + + Can these be made by changing the order of lines and/or deleting lines in the draw function? + + .. figure:: Figures/snowflake1.jpg + :width: 200px + :align: center + :alt: Boxy Snowflake - 1 + :figclass: align-center + + .. figure:: Figures/snowflake2.jpg + :width: 200px + :align: center + :alt: Boxy Snowflake - 2 + :figclass: align-center + + + - 1 is real + + - Wrong! + + - 2 is real + + - Wrong! + + - Both are shopped + + + Correct! + + - Both are real + + - Wrong! + +.. mchoice:: cds_Recursive_maze + :author: Cynthia Lee + + In what order do we visit these spaces? + + .. figure:: Figures/Maze-solving.jpg + :width: 200px + :align: center + :alt: Maze-solving + :figclass: align-center + + - x1, x2, x3 + + - Wrong! + + - x2, x3, x1 + + - Wrong! + + - x1, x3, x2 + + - Wrong! + + - We don’t visit all three + + + Correct! We can't visit x2. + + +.. mchoice:: cds_Recursive_stack + :author: Cynthia Lee + + What is the deepest the Stack gets during the solving of this maze? + + .. figure:: Figures/stack.jpg + :width: 200px + :align: center + :alt: The stack + :figclass: align-center + + - Less than 5 + + - Wrong! + + - 5-10 + + - Wrong! + + - 11-20 + + - Wrong! + + - More than 20 + + + Correct! + +.. mchoice:: cds_Recursive_fibonacci + :author: Cynthia Lee + + How many times would we calculate Fib(2) while calculating Fib(6)? See if you can just “read” it off the chart above. + + .. figure:: Figures/fibonacci.jpg + :width: 200px + :align: center + :alt: fibonacci + :figclass: align-center + + - 4 times + + - Wrong! + + - 5 times + + + Correct! + + - 6 times + + - Wrong! + +.. mchoice:: cds_Recursive_fibonacci-2 + :author: Cynthia Lee + + Assume we have to calculate each unique function call once, but never again. And We “remember” the answer from the first time. + How many rectangles remain in the above chart for n=5? + + .. figure:: Figures/fibonacci.jpg + :width: 200px + :align: center + :alt: fibonacci + :figclass: align-center + + - 3 + + - Wrong! + + - 6 + + - Wrong! Are there any other possibilities? + + - 7 + + - Wrong! + + - 9 + + - Wrong! Are there any other possibilities? + + - More than one. + + + Correct! 6 or 9 + +.. mchoice:: cds_Recursive_fibonacci-3 + :author: Cynthia Lee + + Assume we have to calculate each unique function call once, but never again. And We “remember” the answer from the first time. + For some integer n>2, what is the largest number of function calls that can be triggered by the calculation of F(n) in this new “remember” system? + + .. figure:: Figures/fibonacci.jpg + :width: 200px + :align: center + :alt: fibonacci + :figclass: align-center + + - Approx. log(n) + + - Wrong! + + - Approx. n + + + Correct! + + - Approx. n^2 + + - Wrong! + + - Approx. 2^n + + - Wrong! + +.. mchoice:: cds_Recursive_memory + :author: Cynthia Lee + + How does this look in memory? + + :: + + long factorial (int n) { + cout << n << endl; + long result; + if (n == 1) result = 1; + else result = n * factorial(n – 1); + return result; + } + void myfunction(){ + int x = 10; + long xfac = 0; + xfac = factorial(x); + } + + .. figure:: Figures/memory1.jpg + :width: 200px + :align: center + :alt: memory + :figclass: align-center + + - .. figure:: Figures/memory2.jpg + :width: 200px + :align: center + :alt: memory + :figclass: align-center + + - Wrong! + + - .. figure:: Figures/memory3.jpg + :width: 200px + :align: center + :alt: memory + :figclass: align-center + + + Correct! + + - .. figure:: Figures/memory4.jpg + :width: 200px + :align: center + :alt: memory + :figclass: align-center + + - Wrong! + + diff --git a/_sources/Recursion/Figures/Maze-solving.jpg b/_sources/Recursion/Figures/Maze-solving.jpg new file mode 100644 index 00000000..f7ccac0f Binary files /dev/null and b/_sources/Recursion/Figures/Maze-solving.jpg differ diff --git a/_sources/Recursion/Figures/fibonacci.jpg b/_sources/Recursion/Figures/fibonacci.jpg new file mode 100644 index 00000000..69017045 Binary files /dev/null and b/_sources/Recursion/Figures/fibonacci.jpg differ diff --git a/_sources/Recursion/Figures/memory1.jpg b/_sources/Recursion/Figures/memory1.jpg new file mode 100644 index 00000000..1f4e82f6 Binary files /dev/null and b/_sources/Recursion/Figures/memory1.jpg differ diff --git a/_sources/Recursion/Figures/memory2.jpg b/_sources/Recursion/Figures/memory2.jpg new file mode 100644 index 00000000..b374acca Binary files /dev/null and b/_sources/Recursion/Figures/memory2.jpg differ diff --git a/_sources/Recursion/Figures/memory3.jpg b/_sources/Recursion/Figures/memory3.jpg new file mode 100644 index 00000000..c9736bbd Binary files /dev/null and b/_sources/Recursion/Figures/memory3.jpg differ diff --git a/_sources/Recursion/Figures/memory4.jpg b/_sources/Recursion/Figures/memory4.jpg new file mode 100644 index 00000000..64fb7498 Binary files /dev/null and b/_sources/Recursion/Figures/memory4.jpg differ diff --git a/_sources/Recursion/Figures/nowflake2.jpg b/_sources/Recursion/Figures/nowflake2.jpg new file mode 100644 index 00000000..511c65de Binary files /dev/null and b/_sources/Recursion/Figures/nowflake2.jpg differ diff --git a/_sources/Recursion/Figures/snowflake.jpg b/_sources/Recursion/Figures/snowflake.jpg new file mode 100644 index 00000000..69a525f1 Binary files /dev/null and b/_sources/Recursion/Figures/snowflake.jpg differ diff --git a/_sources/Recursion/Figures/snowflake1.jpg b/_sources/Recursion/Figures/snowflake1.jpg new file mode 100644 index 00000000..64e618bf Binary files /dev/null and b/_sources/Recursion/Figures/snowflake1.jpg differ diff --git a/_sources/Recursion/Figures/stack.jpg b/_sources/Recursion/Figures/stack.jpg new file mode 100644 index 00000000..42d0ceb2 Binary files /dev/null and b/_sources/Recursion/Figures/stack.jpg differ diff --git a/_sources/Recursion/toctree.rst b/_sources/Recursion/toctree.rst index 1c0159f0..53cee6b4 100644 --- a/_sources/Recursion/toctree.rst +++ b/_sources/Recursion/toctree.rst @@ -20,3 +20,5 @@ Recursion DiscussionQuestions.rst pythondsProgrammingExercises.rst Glossary.rst + Exercises.rst + diff --git a/_sources/SearchHash/Exercises.rst b/_sources/SearchHash/Exercises.rst new file mode 100644 index 00000000..0b0ba56d --- /dev/null +++ b/_sources/SearchHash/Exercises.rst @@ -0,0 +1,370 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + +.. mchoice:: cds_Hash_quick + :author: Cynthia Lee + + Can we do binary search on a linked list to implement a quick insert? + + + - It’s a great idea as long as you have a doubly-linked list + + - Wrong! + + - Even with a doubly-linked list the performance is not good, because linked list nodes are strewn all over the heap so you can’t jump right to a middle one + + + Correct! + + - It’s a great idea as long as you make sure that your linked list nodes follow one another in heap memory with no gaps so you can jump to the middle one + + - Wrong! + + + +.. mchoice:: cds_Harsh_BSTput + :author: Cynthia Lee + + Insert: 22, 9, 34, 18, 3 + How many of these result in the same tree structure as above? + 22, 34, 9, 18, 3 + 22, 18, 9, 3, 34 + 22, 9, 3, 18, 34 + + + - None of these + + - Wrong! + + - 1 of these + + - Wrong! + + - 2 of these + + + Correct! + + - ALL of these + + - Wrong! + +.. mchoice:: cds_Harsh_badBST + :author: Cynthia Lee + + How many distinctly structured BSTs are there that exhibit the worst-case height (height equals the number of nodes) for a tree with 5 nodes? + + + - 2-3 + + - Wrong! + + - 4-5 + + - Wrong! + + - 6-7 + + - Wrong! + + - 8-9 + + - Wrong! + + - more than 9 + + + Correct! 2^4 = 16 + +.. mchoice:: cds_Harsh_bestBST + :author: Cynthia Lee + + What is the BEST CASE cost for doing containsKey() in BST? + + + - O(1) + + - Wrong! + + - O(log n) + + + Correct! In the best-case scenario, the binary search tree would be a balanced binary search tree. So the height of the search three becomes log(n), therefore the best case time complexity is O(log n). + + - O(n) + + - Wrong! + + - O(n log n) + + - Wrong! + + - O(n^2) + + - Wrong! + +.. mchoice:: cds_Harsh_worstBST + :author: Cynthia Lee + + What is the WORST CASE cost for doing containsKey() in BST? + + + - O(1) + + - Wrong! + + - O(log n) + + - Wrong! + + - O(n) + + + Correct! in the worst-case scenario, we can have a tree with worst-case height and the element that we have to search for maybe at the very "end" or "bottom" of the tree, so we would have to traverse through all of the nodes to search for that element. Therefore, we would have an O(n) run-time complexity in the worst-case scenario. + + - O(n log n) + + - Wrong! + + - O(n^2) + + - Wrong! + +.. mchoice:: cds_Harsh_worstBST + :author: Cynthia Lee + + What is the WORST CASE cost for doing containsKey() in BST if the BST is complete?? + + + - O(1) + + - Wrong! + + - O(log n) + + + Correct! Every complete BST is balanced. Therefore, searching for a key in a complete BST would have O(log n) complexity. + + - O(n) + + - Wrong! + + - O(n log n) + + - Wrong! + + - O(n^2) + + - Wrong! + +.. mchoice:: cds_Harsh_closedaddressing + :author: Cynthia Lee + + Where does key=“Annie” value=55 go if hashkey(“Annie”) = 3? + + - 55 overwrites 10 at 3 + + - Wrong! + + - A link list node is added at 3 + + + Correct! + + - Other/none/ more than one + + - Wrong! + +.. mchoice:: cds_Harsh_hashmap + :author: Cynthia Lee + + We don’t actually need to store the keys in the nodes—we only really need the key to find which index, then store the value there.TRUE or FALSE + + - TRUE + + - Wrong! + + - FALSE + + + Correct! + + .. mchoice:: cds_Harsh_print + :author: Cynthia Lee + + What does this print? + + .. figure:: Figures/Harshprint.jpg + :width: 200px + :align: center + :alt: Harsh_print + :figclass: align-center + :: + + void traverse(Node *node) { + if (node != NULL) { + cout << node->key << " "; + traverse(node->left); + traverse(node->right); + } + } + + + - A B C D E F + + - Wrong! + + - A B D E C F + + + Correct! + + - D B E F C A + + - Wrong! + + - D E B F C A + + - Wrong! + +.. mchoice:: cds_Harsh_print2 + :author: Cynthia Lee + + What does this print? + + .. figure:: Figures/Harshprint.jpg + :width: 200px + :align: center + :alt: Harsh_print + :figclass: align-center + :: + + void traverse(Node *node) { + if (node != NULL) { + traverse(node->left); + cout << node->key << " "; + traverse(node->right); + } + } + + + + - A B C D E F + + - Wrong! + + - A B D E C F + + - Wrong! + + - D B E F C A + + - Wrong! + + - D E B F C A + + - Wrong! + + - D B E A C F + + + Correct! + +.. mchoice:: cds_Harsh_Pprintorder + :author: Cynthia Lee + + + How can we get this to print our ABCs in order? + + .. figure:: Figures/Harshprint.jpg + :width: 200px + :align: center + :alt: Harsh_print + :figclass: align-center + + - You can’t—we already tried moving the cout to all 3 possible places. + + - Wrong! + + - You can but you use a stack instead of recursion. + + - Wrong! + + - You can but you use a queue instead of recursion. + + + Correct! + + - Other/none/more + + - Wrong! + +.. mchoice:: cds_Harsh_printorder2 + :author: Cynthia Lee + + That prints the ABC nodes in order, but notice that our ABC tree isn’t a BST. How do we print BST nodes in order? + + .. figure:: Figures/Harshprintorder2.jpg + :width: 200px + :align: center + :alt: Harsh_print + :figclass: align-center + :: + + int traverse(Node *node) { + if (node != NULL) { + traverse(node->left); + traverse(node->right); + } + } + + + + - Use the BFS (queue instead of recursion). + + - Wrong! + + - Use the DFS and put cout first. + + - Wrong! + + - Use the DFS and put cout between. + + + Correct! + + - Use the DFS and put cout last. + + - Wrong! + + +.. mchoice:: cds_Harsh_print3 + :author: Cynthia Lee + + We can play the same game with non-alpha characters as keys: What does this print? + + .. figure:: Figures/Harshprint3.jpg + :width: 200px + :align: center + :alt: Harsh_print + :figclass: align-center + :: + + int traverse(Node *node) { + if (node != NULL) { + int l = traverse(node->left); + int r = traverse(node->right); + return l + r + } + } + + + + - * + / 3 4 8 2 + + - Wrong! + + - * + 3 4 / 2 + + - Wrong! + + - 3 + 4 * 8 / 2 + + - Wrong! + + - 3 4 + 8 2 / * + + + Correct! + diff --git a/_sources/SearchHash/Figures/Harshprint.jpg b/_sources/SearchHash/Figures/Harshprint.jpg new file mode 100644 index 00000000..1fc72e0e Binary files /dev/null and b/_sources/SearchHash/Figures/Harshprint.jpg differ diff --git a/_sources/SearchHash/Figures/Harshprint3.jpg b/_sources/SearchHash/Figures/Harshprint3.jpg new file mode 100644 index 00000000..e4df8109 Binary files /dev/null and b/_sources/SearchHash/Figures/Harshprint3.jpg differ diff --git a/_sources/SearchHash/Figures/Harshprintorder2.jpg b/_sources/SearchHash/Figures/Harshprintorder2.jpg new file mode 100644 index 00000000..56b22847 Binary files /dev/null and b/_sources/SearchHash/Figures/Harshprintorder2.jpg differ diff --git a/_sources/SearchHash/toctree.rst b/_sources/SearchHash/toctree.rst index c0c34e39..95706ecc 100644 --- a/_sources/SearchHash/toctree.rst +++ b/_sources/SearchHash/toctree.rst @@ -14,3 +14,5 @@ Searching and Hashing DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst + Exercises.rst + diff --git a/_sources/Sort/Exercises.rst b/_sources/Sort/Exercises.rst new file mode 100644 index 00000000..e9826f73 --- /dev/null +++ b/_sources/Sort/Exercises.rst @@ -0,0 +1,73 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + +.. mchoice:: cds_Sort_secondout + :author: Cynthia Lee + + I would like to move one paper from the two sorted sub-piles to the combined sorted pile. How many papers do I have to examine, at most, to identify the current smallest element of both sorted sub-piles? + + + - 2 elements + + - Wrong! + + - 4 elements + + + For you to identify the smallest of the two sorted sub-piles, you need to check the "top-most" and "bottom-most" elements of each pile so that would mean that you would examine 4 elements total. + + - Size of the first pile + + - Wrong! + + - Size of the second pile + + - Wrong! + +.. mchoice:: cds_Sort_secondout + :author: Cynthia Lee + + How many steps does it take to fully merge two sorted sub-piles, A and B? (best/tight answer) + + + - O(log(A+B)) steps + + - Wrong! + + - O(A+B) steps + + + Correct! + + - O(A+B)^2 steps + + - Wrong! + + - O(A^2 + B^2) steps + + - Wrong! + +.. mchoice:: cds_Sort_secondout + :author: Cynthia Lee + + Consider an arbitrarily chosen (generic) particular exam and mentally track its progress throughout the algorithm. How many times does your exam pass through the merge step? + + + - 1 time + + - Wrong! + + - 2 times + + + Correct! + + - Θ(logn) times + + - Wrong! + + - Θ(n) times + + - Wrong! + diff --git a/_sources/Sort/toctree.rst b/_sources/Sort/toctree.rst index 257e2fae..2b5d5b45 100644 --- a/_sources/Sort/toctree.rst +++ b/_sources/Sort/toctree.rst @@ -17,3 +17,4 @@ Sorting DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst + Exercises.rst diff --git a/_sources/Trees/Exercises.rst b/_sources/Trees/Exercises.rst new file mode 100644 index 00000000..f2740bac --- /dev/null +++ b/_sources/Trees/Exercises.rst @@ -0,0 +1,272 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + +.. mchoice:: cds_tree_remove + :author: Cynthia Lee + + Suppose we have a pointer (current) to the node containing the item to be removed. + What additional information do we need to successfully remove the node? + + + .. figure:: Figures/remove.jpg + :width: 200px + :align: center + :alt: remove + :figclass: align-center + + - Nothing additional. + + - Wrong! + + - A pointer to the node immediately prior to the to-be-deleted node. + + + Correct! + + - A pointer to the node immediately after the to-be-deleted node. + + - Wrong! + + - Both B and C. + + - Wrong! + +.. mchoice:: cds_tree_binarytree + :author: Cynthia Lee + + How many of these are valid binary trees? + + + .. figure:: Figures/binarytree.jpg + :width: 200px + :align: center + :alt: binary-trees + :figclass: align-center + + - 0-3 + + - Wrong! + + - 7-8 + + + Correct! 1,2,3,4,5,6,8 are correct + + - 4-6 + + - Wrong! + +.. mchoice:: cds_tree_binaryheap + :author: Cynthia Lee + + How many of these are valid binary heaps? + + + .. figure:: Figures/binaryheap.jpg + :width: 200px + :align: center + :alt: binary-heaps + :figclass: align-center + + - 0-1 + + - Wrong! + + - 4 + + + Correct! 1,2,5,8 are correct + + - 2-3 + + - Wrong! + +.. mchoice:: cds_tree_minbinaryheap + :author: Cynthia Lee + + How many of these are valid min-binary-heaps? + + + .. figure:: Figures/minbinaryheap.jpg + :width: 200px + :align: center + :alt: min-binary-heaps + :figclass: align-center + + - 0 + + - Wrong! + + - 1 + + + Correct! The 2nd one is correct + + - 2 + + - Wrong! + + - 3 + + - Wrong! + +.. mchoice:: cds_tree_minbinaryheap2 + :author: Cynthia Lee + + In how many places could the smallest number in this min-binary-heap be located? + + + .. figure:: Figures/minbinaryheap2.jpg + :width: 200px + :align: center + :alt: min-binary-heaps + :figclass: align-center + + - 0-2 + + - Wrong! + + - 3-4 + + + Correct! The 2nd one is correct + + - 5-6 + + - Wrong! + + - 7-8 + + - Wrong! + +.. mchoice:: cds_tree_arrayheap + :author: Cynthia Lee + + For the tree of height h, the array length is 2^h-1 For a node in array index i: Parent is at array index:? + + + .. figure:: Figures/arrayheap.jpg + :width: 200px + :align: center + :alt: arrayheap + :figclass: align-center + + - i-2 + + - Wrong! + + - (i-1)/2 + + + Correct! + + - i/2 + + - Wrong! + + - 2i + + - Wrong! + +.. mchoice:: cds_tree_arraybinheap + :author: Cynthia Lee + + For the tree of height h, the array length is 2^h-1 For a node in array index i: Left child is at array index:? + + + .. figure:: Figures/arraybinheap.jpg + :width: 200px + :align: center + :alt: arraybinheap + :figclass: align-center + + - i+1 + + - Wrong! + + - i+2 + + - Wrong! + + - 2i+1 + + + Correct! + + - 2i + + - Wrong! + +.. mchoice:: cds_tree_tf + :author: Cynthia Lee + + There is only one configuration of a valid min-heap containing the elements {34, 22, 3, 9, 18} + + + - TRUE + + - Wrong! + + - FALSE + + + Correct! + + .. mchoice:: cds_tree_distict + :author: Cynthia Lee + + How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34}? + + + - 1-2 + + - Wrong! + + - 3-4 + + - Wrong! + + - 5-8 + + + Correct! + + - 5! + + - Wrong! + + .. mchoice:: cds_tree_time + :author: Cynthia Lee + + What is the worst-case time cost for each heap operation: Add, Remove, Peek? + + + - O(n), O(1), O(1) + + - Wrong! + + - O(logn), O(logn), O(1) + + + Correct! + + - O(n), O(logn), O(logn) + + - Wrong! + +.. mchoice:: cds_tree_insert + :author: Cynthia Lee + + What is the next configuration in this sequence? + + .. figure:: Figures/sequence.jpg + :width: 200px + :align: center + :alt: sequence + :figclass: align-center + + - 12, 8, 2, 10 + + - Wrong! + + - 12, 10, 2, 8 + + + Correct! + + - 12, 10, 8, 2 + + - Wrong! diff --git a/_sources/Trees/Figures/arraybinheap.jpg b/_sources/Trees/Figures/arraybinheap.jpg new file mode 100644 index 00000000..ad391d7d Binary files /dev/null and b/_sources/Trees/Figures/arraybinheap.jpg differ diff --git a/_sources/Trees/Figures/arrayheap.jpg b/_sources/Trees/Figures/arrayheap.jpg new file mode 100644 index 00000000..eecdab14 Binary files /dev/null and b/_sources/Trees/Figures/arrayheap.jpg differ diff --git a/_sources/Trees/Figures/binaryheap.jpg b/_sources/Trees/Figures/binaryheap.jpg new file mode 100644 index 00000000..de333fba Binary files /dev/null and b/_sources/Trees/Figures/binaryheap.jpg differ diff --git a/_sources/Trees/Figures/binarytree.jpg b/_sources/Trees/Figures/binarytree.jpg new file mode 100644 index 00000000..344e1605 Binary files /dev/null and b/_sources/Trees/Figures/binarytree.jpg differ diff --git a/_sources/Trees/Figures/minbinaryheap.jpg b/_sources/Trees/Figures/minbinaryheap.jpg new file mode 100644 index 00000000..52888e1a Binary files /dev/null and b/_sources/Trees/Figures/minbinaryheap.jpg differ diff --git a/_sources/Trees/Figures/minbinaryheap2.jpg b/_sources/Trees/Figures/minbinaryheap2.jpg new file mode 100644 index 00000000..3d5a9a51 Binary files /dev/null and b/_sources/Trees/Figures/minbinaryheap2.jpg differ diff --git a/_sources/Trees/Figures/remove.jpg b/_sources/Trees/Figures/remove.jpg new file mode 100644 index 00000000..8215774c Binary files /dev/null and b/_sources/Trees/Figures/remove.jpg differ diff --git a/_sources/Trees/Figures/sequence.jpg b/_sources/Trees/Figures/sequence.jpg new file mode 100644 index 00000000..81f17256 Binary files /dev/null and b/_sources/Trees/Figures/sequence.jpg differ diff --git a/_sources/Trees/toctree.rst b/_sources/Trees/toctree.rst index 26b6f4d4..33558384 100644 --- a/_sources/Trees/toctree.rst +++ b/_sources/Trees/toctree.rst @@ -27,3 +27,4 @@ Trees and Tree Algorithms DiscussionQuestions.rst ProgrammingExercises.rst Glossary.rst + Exercises.rst diff --git a/allChapterFiles.txt b/allChapterFiles.txt index 914b98a9..35ccf8e7 100644 --- a/allChapterFiles.txt +++ b/allChapterFiles.txt @@ -12,7 +12,7 @@ Introduction/ExceptionHandling.rst Introduction/DefiningFunctions.rst Introduction/ObjectOrientedProgramminginPythonDefiningClasses.rst - Introduction/.rst + Introduction/Exercises.rst Introduction/Summary.rst Introduction/KeyTerms.rst Introduction/DiscussionQuestions.rst @@ -79,7 +79,7 @@ BasicDS/BalancedSymbols(AGeneralCase).rst BasicDS/ConvertingDecimalNumberstoBinaryNumbers.rst BasicDS/InfixPrefixandPostfixExpressions.rst - BasicDS/.rst + BasicDS/Exercises.rst diff --git a/where should I placed these questions?.txt b/where should I placed these questions?.txt new file mode 100644 index 00000000..61e0583c --- /dev/null +++ b/where should I placed these questions?.txt @@ -0,0 +1,7 @@ +Multiple Choice Exercises +------------------------- + +Answer the following **Multiple Choice** questions to +assess what you have learned in this chapter. + + \ No newline at end of file