You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 11_further_object-oriented_features.html
+93-3Lines changed: 93 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -721,12 +721,102 @@ <h3><span class="section-number">11.5.2. </span>Support while revising<a class="
721
721
</div>
722
722
<divclass="section" id="practice-questions">
723
723
<h3><spanclass="section-number">11.5.3. </span>Practice questions<aclass="headerlink" href="#practice-questions" title="Permalink to this headline">¶</a></h3>
724
-
<p>Some specifically-designed practice questions will be released at about the end
725
-
of term. In addition to this, there are a lot of very good exercises in
724
+
<p>Some specifically-designed practice questions are presented below. In addition to this, there are a lot of very good exercises in
726
725
chapters 7 and 9 of <aclass="reference external" href="https://link.springer.com/book/10.1007%2F978-3-662-49887-3">Hans Petter Langtangen, A Primer on Scientific Programming
727
726
with Python</a>.
728
727
You can access that book by logging in with your Imperial credentials.</p>
729
-
<pclass="rubric">Footnotes</p>
728
+
<p>The first two questions are in exam format.</p>
729
+
<divclass="proof proof-type-exercise" id="id9">
730
+
731
+
<divclass="proof-title">
732
+
<spanclass="proof-type">Exercise 11.3</span>
733
+
734
+
</div><divclass="proof-content">
735
+
<p>Obtain the <aclass="reference external" href="https://classroom.github.com/a/HdgipMxw">practice problem from GitHub Classroom</a>. Follow the instructions in
736
+
the README file that will be displayed on GitHub on your copy of the page.</p>
737
+
<divclass="admonition note">
738
+
<pclass="admonition-title">Note</p>
739
+
<p>This exercise is fully set up as an exam question, including provisional
740
+
points on the autotests. It should be doable in 30 minutes, though the
741
+
level of programming is a little more basic than the exam questions.</p>
<p>Obtain the <aclass="reference external" href="https://classroom.github.com/a/6usAsES4">practice problem from GitHub Classroom</a>. Follow the instructions in
750
+
the README file that will be displayed on GitHub on your copy of the page.</p>
751
+
<divclass="admonition note">
752
+
<pclass="admonition-title">Note</p>
753
+
<p>This exercise is at the level of an exam question, though longer. An
754
+
actual exam question would be pruned back to be achievable in 30
755
+
minutes. Here the complete exercise is presented because the main thing
756
+
you need to do is practice programming, and cutting out material
757
+
doesn’t help with that. Marks are not given as the question is the
758
+
wrong length, so dividing 20 marks over the question would just be
759
+
misleading</p>
760
+
</div>
761
+
</div></div><p>In addition to these exam-style questions, you can also usefully practice
762
+
programming by going beyond the specification of the exercises in the course.
763
+
The following exercises are just ideas for how to do that. They do not come
764
+
with additional code or tests.</p>
765
+
<divclass="proof proof-type-exercise" id="id11">
766
+
767
+
<divclass="proof-title">
768
+
<spanclass="proof-type">Exercise 11.5</span>
769
+
770
+
</div><divclass="proof-content">
771
+
<p>Extend the <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Polynomial</span></code> class from <aclass="reference internal" href="3_objects.html#objects"><spanclass="std std-numref">Week 3</span></a> to
772
+
support polynomial division. Polynomial division results in a quotient and
773
+
a remainder, so you might choose to implement <aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__floordiv__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__floordiv__()</span></code></a>
774
+
to return the quotient and <aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__mod__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__mod__()</span></code></a> to return the remainder,
775
+
in a manner analogous to integer division. You might also implement
776
+
<aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__truediv__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__truediv__()</span></code></a> and have it return the quotient if the
777
+
polynomial division is exact, but raise <aclass="reference external" href="https://docs.python.org/3/library/exceptions.html#ValueError" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">ValueError</span></code></a> if there is a
778
+
remainder.</p>
779
+
<divclass="admonition hint">
780
+
<pclass="admonition-title">Hint</p>
781
+
<p>Don’t forget that repeating code is poor style, so you might need a
782
+
helper method to implement the actual polynomial division.</p>
<p>Extend the <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Deque</span></code> class from <aclass="reference internal" href="5_abstract_data_types.html#abstract-data-types"><spanclass="std std-numref">Week 5</span></a> to automatically resize the ring buffer by a
791
+
proportion of its length when it is full, and when it becomes too empty.
792
+
You can check the behaviour of your implementation against
<p>For a real challenge, extend the groups implementation from <aclass="reference internal" href="8_inheritance.html#inheritance"><spanclass="std std-numref">Week
801
+
8</span></a> to support taking the quotient of two groups. What do the
802
+
values and validation of a quotient group look like in code? You could
803
+
implement <aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__truediv__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__truediv__()</span></code></a> on <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Group</span></code> to provide the user
<p>Write additional single dispatch visitor functions to extend the
812
+
capabilities of the symbolic algebra system you wrote in <aclass="reference internal" href="10_trees_and_directed_acyclic_graphs.html#trees"><spanclass="std std-numref">Week 10</span></a>. You could, for example, write a visitor which performs
813
+
cancellation of expressions involving 1 or 0. You could implement expansion
814
+
of brackets according to distributive laws. Finally you could canonicalise
815
+
commutative operators such as <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">+</span></code> and <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">*</span></code> so that, for example <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">1</span><spanclass="pre">+</span><spanclass="pre">x</span></code> is
816
+
mapped to <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">x</span><spanclass="pre">+</span><spanclass="pre">1</span></code>. Doing this over multiple layers of the tree
817
+
(transforming <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">1</span><spanclass="pre">+</span><spanclass="pre">2*x</span><spanclass="pre">+</span><spanclass="pre">3*x**2</span></code> to <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">3*x**2</span><spanclass="pre">+</span><spanclass="pre">2*x</span><spanclass="pre">+</span><spanclass="pre">1</span></code> is an additional
<dd><p>Most of the <aclass="reference external" href="https://docs.python.org/3/library/index.html#library-index" title="(in Python v3.9)"><spanclass="xref std std-ref">Python Standard Library</span></a> is written
0 commit comments