Skip to content

Commit

Permalink
Fixed issue pearcej#587 - Missing Matching section in cppds2, chapter 6.
Browse files Browse the repository at this point in the history
  • Loading branch information
seedyjahateh committed May 23, 2024
1 parent f69c1a7 commit 725176b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 86 deletions.
172 changes: 86 additions & 86 deletions pretext/LinearBasic/ImplementingaStackCpp.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -9,93 +9,93 @@
type such as a stack is the creation of a new class. The stack
operations are implemented as methods. However, the STL already has a well
written implementation of the Stack class.</p>
<p>The following stack implementation (<xref ref="lst-stackcode1"/>) assumes that
the end of the array will hold the top element of the stack. As the stack
grows (as <c>push</c> operations occur), new items will be added on the end
of the array. <c>pop</c> operations will manipulate that same end.</p>

<TabNode tabname="C++" tabnode_options="{'subchapter': 'ImplementingaStackCpp', 'chapter': 'LinearBasic', 'basecourse': 'cppds', 'optional': '', 'optclass': '', 'tabname': 'C++'}">

<program xml:id="stack_1ac_cpp" interactive="activecode" language="cpp">
<input>
//Tests the push, empty, size, pop, and top methods of the stack library.

#include &lt;iostream&gt;
#include &lt;stack&gt; // Calling Stack from the STL

using namespace std;

int main() {
stack&lt;int&gt; newStack;

newStack.push(3); //Adds 3 to the stack
newStack.push(8);
newStack.push(15);

// returns a boolean response depending on if the stack is empty or not
cout &lt;&lt; "Stack Empty? " &lt;&lt; newStack.empty() &lt;&lt; endl;

// returns the size of the stack itself
cout &lt;&lt; "Stack Size: " &lt;&lt; newStack.size() &lt;&lt; endl;

// returns the topmost element of the stack
cout &lt;&lt; "Top Element of the Stack: " &lt;&lt; newStack.top() &lt;&lt; endl;

// removes the topmost element of the stack
newStack.pop();

cout &lt;&lt; "Top Element of the Stack: " &lt;&lt; newStack.top() &lt;&lt; endl;

cout &lt;&lt; "Stack Size: " &lt;&lt; newStack.size() &lt;&lt; endl;

return 0;
}
</input>
</program>
</TabNode><TabNode tabname="Python" tabnode_options="{'subchapter': 'ImplementingaStackCpp', 'chapter': 'LinearBasic', 'basecourse': 'cppds', 'optional': '', 'optclass': '', 'tabname': 'Python'}">

<program xml:id="stack_1ac_py" interactive="activecode" language="python">
<input>
#Tests the push, empty, size, pop, and top methods of the stack library.

class Stack:
def __init__(self): #initializes new stack instance
self.items = []

def isEmpty(self): #returns boolean
return self.items == []

def push(self, item): #pushes new item onto stack
self.items.append(item)

def pop(self): #removes topmost item from stack
return self.items.pop()

def top(self): #returns the topmost item from the stack
return self.items[len(self.items)-1]

def size(self): #returns the size of the stack
return len(self.items)

def main():
newStack = Stack()
newStack.push(4)
newStack.push(8)
newStack.push(15)

print("Stack Empty? ", newStack.isEmpty())

print("Stack Size: ", newStack.size())

print("Top Element of the Stack: ", newStack.top())

newStack.pop();

print("Top Element of the Stack: ", newStack.top())
main()
</input>
</program>
<p>The following stack implementation <a href="#stack_1ac_cpp">ActiveCode - Stack Implementation</a> assumes that the end of the array will hold the top element of the stack. As the stack grows (as <c>push</c> operations occur), new items will be added on the end of the array. <c>pop</c> operations will manipulate that same end. If the link is not working, please scroll down to the corresponding section manually or refer to the program ID: stack_1ac_cpp.</p>


<TabNode tabname="C++" tabnode_options="{'subchapter': 'ImplementingaStackCpp', 'chapter': 'LinearBasic', 'basecourse': 'cppds', 'optional': '', 'optclass': '', 'tabname': 'C++'}">
<program xml:id="stack_1ac_cpp" interactive="activecode" language="cpp">
<input>
// Tests the push, empty, size, pop, and top methods of the stack library.

#include &lt;iostream&gt;
#include &lt;stack&gt; // Calling Stack from the STL

using namespace std;

int main() {
stack&lt;int&gt; newStack;

newStack.push(3); // Adds 3 to the stack
newStack.push(8);
newStack.push(15);

// Returns a boolean response depending on if the stack is empty or not
cout &lt;&lt; "Stack Empty? " &lt;&lt; newStack.empty() &lt;&lt; endl;

// Returns the size of the stack itself
cout &lt;&lt; "Stack Size: " &lt;&lt; newStack.size() &lt;&lt; endl;

// Returns the topmost element of the stack
cout &lt;&lt; "Top Element of the Stack: " &lt;&lt; newStack.top() &lt;&lt; endl;

// Removes the topmost element of the stack
newStack.pop();

cout &lt;&lt; "Top Element of the Stack: " &lt;&lt; newStack.top() &lt;&lt; endl;

cout &lt;&lt; "Stack Size: " &lt;&lt; newStack.size() &lt;&lt; endl;

return 0;
}
</input>
</program>
</TabNode>

<TabNode tabname="Python" tabnode_options="{'subchapter': 'ImplementingaStackCpp', 'chapter': 'LinearBasic', 'basecourse': 'cppds', 'optional': '', 'optclass': '', 'tabname': 'Python'}">
<program xml:id="stack_1ac_py" interactive="activecode" language="python">
<input>
# Tests the push, empty, size, pop, and top methods of the stack library.

class Stack:
def __init__(self): # Initializes new stack instance
self.items = []

def isEmpty(self): # Returns boolean
return self.items == []

def push(self, item): # Pushes new item onto stack
self.items.append(item)

def pop(self): # Removes topmost item from stack
return self.items.pop()

def top(self): # Returns the topmost item from the stack
return self.items[-1]

def size(self): # Returns the size of the stack
return len(self.items)

def main():
newStack = Stack()
newStack.push(4)
newStack.push(8)
newStack.push(15)

print("Stack Empty? ", newStack.isEmpty())

print("Stack Size: ", newStack.size())

print("Top Element of the Stack: ", newStack.top())

newStack.pop()

print("Top Element of the Stack: ", newStack.top())

main()
</input>
</program>
</TabNode>


<reading-questions xml:id="rq-stack-implementation">
<title>Reading Questions</title>
Expand Down
30 changes: 30 additions & 0 deletions pretext/SearchHash/Matching.ptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<section>
<title>Matching</title>
<exercise label="matching_searchhash">
<statement><p>Drag the word on the left to its corresponding definition.</p></statement>
<feedback><p>Review classes and their properties.</p></feedback>
<matches>
<match order="1"><premise>binary search</premise><response>One repeatedly divides a sorted data structure in half and determines if the item is in one half of it until the item is found or deemed not in the data.</response></match>
<match order="2"><premise>chaining</premise><response>Collision resolution method, in which each slot in a hash table holds a reference to a collection of items.</response></match>
<match order="3"><premise>collision</premise><response>Having two or more items sharing the same slot in a hash table.</response></match>
<match order="4"><premise>collision resolution</premise><response>Systematic method for solving hash table collisions.</response></match>
<match order="5"><premise>clustering</premise><response>Items being mapped in a hash table near each other resulting in items with collisions being put together.</response></match>
<match order="6"><premise>folding method</premise><response>Constructing a hash function by dividing the item into equally sized pieces, adding the pieces together to get a hash value, dividing by the size of the hash table, and the remainder becomes the slot for that item.</response></match>
<match order="7"><premise>hashing</premise><response>Creating a value for an input that can be used to find the input by searching for the value.</response></match>
<match order="8"><premise>hash function</premise><response>Mapping between an item and its slot in a hash table.</response></match>
<match order="9"><premise>linear probing</premise><response>Open addressing technique in which each slot is visited one at a time systematically.</response></match>
<match order="10"><premise>load factor</premise><response>It's the number of items in a hash table divided by the size of the table.</response></match>
<match order="11"><premise>map</premise><response>Associate data type that stores key-data pairs.</response></match>
<match order="12"><premise>mid-square method</premise><response>Method for constructing a hash function by squaring the item and then using some portion of the result.</response></match>
<match order="13"><premise>open addressing</premise><response>Collision resolution that tries to find the next open slot/address in the hash table.</response></match>
<match order="14"><premise>perfect hash function</premise><response>Hash function that maps each item to a unique hash slot.</response></match>
<match order="15"><premise>quadratic probing</premise><response>Variation of linear probing in which rehashing is done using successive squared values.</response></match>
<match order="16"><premise>rehashing</premise><response>Putting an item into a hash table after a collision.</response></match>
<match order="17"><premise>searching</premise><response>Algorithmic process of finding a particular item in a collection of items.</response></match>
<match order="18"><premise>sequential search</premise><response>Search method in which one follows the underlying ordering of items in a collection of data to find a specific item.</response></match>
<match order="19"><premise>slot</premise><response>Position in a hash table.</response></match>
</matches>
</exercise>

</section>

1 change: 1 addition & 0 deletions pretext/SearchHash/toctree.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<xi:include href='./DiscussionQuestions.ptx' />
<xi:include href='./ProgrammingExercises.ptx' />
<xi:include href='./Glossary.ptx' />
<xi:include href='./Matching.ptx' />
</chapter>

0 comments on commit 725176b

Please sign in to comment.