|
96 | 96 | structures which could be used to implement them.</p> |
97 | 97 | <div class="section" id="stacks"> |
98 | 98 | <span id="id2"></span><h2><span class="section-number">5.1. </span>Stacks<a class="headerlink" href="#stacks" title="Permalink to this headline">¶</a></h2> |
99 | | -<p>Possibly the simplest abstract data type which is not synonymous with |
| 99 | +<details class="sphinx-bs dropdown card mb-3"> |
| 100 | +<summary class="summary-title card-header"> |
| 101 | +Video: stacks as an abstract data type.<div class="summary-down docutils"> |
| 102 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 103 | +<div class="summary-up docutils"> |
| 104 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 105 | +</summary><div class="summary-content card-body docutils"> |
| 106 | +<div class="vimeo docutils container"> |
| 107 | +<iframe src="https://player.vimeo.com/video/506479213" |
| 108 | +frameborder="0" allow="autoplay; fullscreen" |
| 109 | +allowfullscreen></iframe></div> |
| 110 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=b014e13a-82ca-4a57-ac7f-acc000e64349">watch this video on Panopto</a>.</p> |
| 111 | +</div> |
| 112 | +</details><p>Possibly the simplest abstract data type which is not synonymous with |
100 | 113 | a Python type is the <a class="reference internal" href="#term-stack"><span class="xref std std-term">stack</span></a>. A stack is a sequence of objects |
101 | 114 | in which only the most recently added object is accessible. The image |
102 | 115 | to have in mind is a stack of plates on a spring-loaded holder of the |
|
113 | 126 | the sequence. <a class="reference internal" href="#stackdiag"><span class="std std-numref">Fig. 5.1</span></a> shows these operations. It is also common to add an additional operation of |
114 | 127 | <code class="xref py py-obj docutils literal notranslate"><span class="pre">peek</span></code>, which returns the most recently added object without removing |
115 | 128 | it from the stack.</p> |
116 | | -<div class="admonition note"> |
117 | | -<p class="admonition-title">Note</p> |
118 | | -<p>The stack operations in the diagram are wrong. The stack is back to front and |
119 | | -the wrong value is popped.</p> |
120 | | -</div> |
121 | 129 | <div class="figure align-default" id="id5"> |
122 | 130 | <span id="stackdiag"></span><div><svg height="740" viewBox="0 0 832 740" width="832" xmlns="http://www.w3.org/2000/svg" xmlns:inkspace="http://www.inkscape.org/namespaces/inkscape" xmlns:xlink="http://www.w3.org/1999/xlink"> |
123 | 131 | <defs id="defs_block"> |
@@ -387,7 +395,20 @@ <h2><span class="section-number">5.2. </span>Separation of concerns<a class="hea |
387 | 395 | </div> |
388 | 396 | <div class="section" id="algorithmic-complexity"> |
389 | 397 | <h2><span class="section-number">5.3. </span>Algorithmic complexity<a class="headerlink" href="#algorithmic-complexity" title="Permalink to this headline">¶</a></h2> |
390 | | -<p>The second reason that understanding abstract data types is important |
| 398 | +<details class="sphinx-bs dropdown card mb-3"> |
| 399 | +<summary class="summary-title card-header"> |
| 400 | +Video: dynamic arrays and algorithmic complexity.<div class="summary-down docutils"> |
| 401 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 402 | +<div class="summary-up docutils"> |
| 403 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 404 | +</summary><div class="summary-content card-body docutils"> |
| 405 | +<div class="vimeo docutils container"> |
| 406 | +<iframe src="https://player.vimeo.com/video/506479208" |
| 407 | +frameborder="0" allow="autoplay; fullscreen" |
| 408 | +allowfullscreen></iframe></div> |
| 409 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=516115a0-b13d-4693-951b-acc000e642ff">watch this video on Panopto</a>.</p> |
| 410 | +</div> |
| 411 | +</details><p>The second reason that understanding abstract data types is important |
391 | 412 | is that a good implementation of a well-designed abstract data type |
392 | 413 | will have well-defined performance characteristics. In particular, the |
393 | 414 | optimal algorithmic complexity, expressed in big <span class="math notranslate nohighlight">\(O\)</span> notation, of |
@@ -648,7 +669,20 @@ <h3><span class="section-number">5.3.1. </span>Amortised complexity and worst ca |
648 | 669 | </div> |
649 | 670 | <div class="section" id="queues-and-deques"> |
650 | 671 | <h2><span class="section-number">5.4. </span>Queues and deques<a class="headerlink" href="#queues-and-deques" title="Permalink to this headline">¶</a></h2> |
651 | | -<p>A <a class="reference internal" href="#term-queue"><span class="xref std std-term">queue</span></a> is, like a <a class="reference internal" href="#term-stack"><span class="xref std std-term">stack</span></a>, an ordered sequence of |
| 672 | +<details class="sphinx-bs dropdown card mb-3"> |
| 673 | +<summary class="summary-title card-header"> |
| 674 | +Video: deques and ring buffers.<div class="summary-down docutils"> |
| 675 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 676 | +<div class="summary-up docutils"> |
| 677 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 678 | +</summary><div class="summary-content card-body docutils"> |
| 679 | +<div class="vimeo docutils container"> |
| 680 | +<iframe src="https://player.vimeo.com/video/506710190" |
| 681 | +frameborder="0" allow="autoplay; fullscreen" |
| 682 | +allowfullscreen></iframe></div> |
| 683 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=5ba7fde3-8ca9-48e2-b66b-acc100bd1953">watch this video on Panopto</a>.</p> |
| 684 | +</div> |
| 685 | +</details><p>A <a class="reference internal" href="#term-queue"><span class="xref std std-term">queue</span></a> is, like a <a class="reference internal" href="#term-stack"><span class="xref std std-term">stack</span></a>, an ordered sequence of |
652 | 686 | objects. The difference is that the only accessible item in the |
653 | 687 | sequence is the <em>earliest</em> added. Items can be added to the back of |
654 | 688 | the queue and taken from the front. As with a stack, the optimal |
@@ -688,7 +722,20 @@ <h3><span class="section-number">5.4.1. </span>Ring buffers<a class="headerlink" |
688 | 722 | </div> |
689 | 723 | <div class="section" id="linked-lists"> |
690 | 724 | <h2><span class="section-number">5.5. </span>Linked lists<a class="headerlink" href="#linked-lists" title="Permalink to this headline">¶</a></h2> |
691 | | -<p>One disadvantage of a deque (and hence of a stack or queue) is that inserting an |
| 725 | +<details class="sphinx-bs dropdown card mb-3"> |
| 726 | +<summary class="summary-title card-header"> |
| 727 | +Video: linked lists.<div class="summary-down docutils"> |
| 728 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 729 | +<div class="summary-up docutils"> |
| 730 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 731 | +</summary><div class="summary-content card-body docutils"> |
| 732 | +<div class="vimeo docutils container"> |
| 733 | +<iframe src="https://player.vimeo.com/video/506743244" |
| 734 | +frameborder="0" allow="autoplay; fullscreen" |
| 735 | +allowfullscreen></iframe></div> |
| 736 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=f37ae26c-a39a-4757-bc0d-acc100eec588">watch this video on Panopto</a>.</p> |
| 737 | +</div> |
| 738 | +</details><p>One disadvantage of a deque (and hence of a stack or queue) is that inserting an |
692 | 739 | object into the middle of the sequence is often an <span class="math notranslate nohighlight">\(O(n)\)</span> operation, |
693 | 740 | because on average half of the items in the sequence need to be shuffled to make |
694 | 741 | space. A linked list provides a mechanism for avoiding this. A singly linked |
@@ -842,7 +889,20 @@ <h2><span class="section-number">5.5. </span>Linked lists<a class="headerlink" h |
842 | 889 | </div> |
843 | 890 | <div class="section" id="the-iterator-protocol"> |
844 | 891 | <span id="iterator-protocol"></span><h2><span class="section-number">5.6. </span>The iterator protocol<a class="headerlink" href="#the-iterator-protocol" title="Permalink to this headline">¶</a></h2> |
845 | | -<p>The abstract data types we have considered here are collections of |
| 892 | +<details class="sphinx-bs dropdown card mb-3"> |
| 893 | +<summary class="summary-title card-header"> |
| 894 | +Video: the iterator protocol.<div class="summary-down docutils"> |
| 895 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-down" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M5.22 8.72a.75.75 0 000 1.06l6.25 6.25a.75.75 0 001.06 0l6.25-6.25a.75.75 0 00-1.06-1.06L12 14.44 6.28 8.72a.75.75 0 00-1.06 0z"></path></svg></div> |
| 896 | +<div class="summary-up docutils"> |
| 897 | +<svg version="1.1" width="24" height="24" class="octicon octicon-chevron-up" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M18.78 15.28a.75.75 0 000-1.06l-6.25-6.25a.75.75 0 00-1.06 0l-6.25 6.25a.75.75 0 101.06 1.06L12 9.56l5.72 5.72a.75.75 0 001.06 0z"></path></svg></div> |
| 898 | +</summary><div class="summary-content card-body docutils"> |
| 899 | +<div class="vimeo docutils container"> |
| 900 | +<iframe src="https://player.vimeo.com/video/506743250" |
| 901 | +frameborder="0" allow="autoplay; fullscreen" |
| 902 | +allowfullscreen></iframe></div> |
| 903 | +<p class="card-text">Imperial students can also <a class="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=d1b2b176-066a-4d68-aa01-acc100eec5c6">watch this video on Panopto</a>.</p> |
| 904 | +</div> |
| 905 | +</details><p>The abstract data types we have considered here are collections of |
846 | 906 | objects, and one common abstract operation which is applicable to |
847 | 907 | collections is to iterate over them. That is to say, to loop over the |
848 | 908 | objects in the collection and perform some action for each one. This |
|
0 commit comments