


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A collection of exercises on object-oriented programming in python, covering topics such as the strategy pattern, vectors, scatter plots, understanding oop, decorator pattern, and extending classes. Students are asked to write code to implement various features and functions, as well as to analyze and extend existing classes.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!
Look again at the slides on the strategy pattern and use the code examples to define the following duck-classes^1 :
Once you defined these classes:
(a) Write a script in which you create the following duck-instances: 3 normal, 3 red- headed, 1 black, 1 rubber and 1 decoy duck. Store all ducks in a list and then call display for each of the ducks.
(b) In your script, let one of the normal ducks ,,break its wings”. (Replace the flying_behavoir of the unlucky duck-object.) Compare what happens when you call the flying- functions of the unlucky duck and one of the other normal ducks.
(c) Change your duck classes such that you can give your individual ducks a name. Use that name when displaying the duck.
(d) [intermediate] Change your duck classes such that they store their position (as a string). Let fly_to change the position and display the present position on take off and landing.
(e) Add more functionality, be creative. (^1) You are of course free to use real duck breeds (see https://en.wikipedia.org/wiki/List_of_duck_
breeds) if you prefer.
The file vector.py contains an implementation of an n-dimensional vector. Some of the functions are not yet complete:
(a) Implement the addition of two vectors via the magic function add. Make sure that the dimensions of the two vectors are aligned.
(b) Do the same for the scalar product with the function mul.
(c) Implement str which is the magic function to represent the vector as string (i.e. str(v)). Define a reasonable string representation.
(d) Create the property length that
(e) Create a subclass for three-dimensional vectors Vector3D with a suitable constructor and implement the magic function matmul (the operator @) as cross-product^2. Important: The implementation should NOT lead to any change in the parent class.
Take pen and paper and design a class representing scatter plots that can be drawn.
The graph module (provided in the archive) contains a set of classes for representing graphs (i.e. nodes and edges connecting them). On a piece of paper reverse engineer its design:
(a) Write down all class names, their methods and data attributes; try to understand what all of them do (read the documentation!).
(b) Figure out how different classes are related. Where is inheritance used, where com- position? Draw a simple diagram.
(^2) The cross-product is defined as v = v 1 × v 2 =
(x 1 y 1 z 1
) ×
(x 2 y 2 z 2
( (^) y 1 z 2 − y 2 z 1 z 1 x 2 − z 2 x 1 x 1 y 2 − x 2 y 1
)
(c) Implement the following city graph as an example:
(d) Now we want to find the quickest path from Berlin to Cologne. Open the shortest path.py file. It contains a SearchAlgorithm class, which implements the Dijkastra algorithm for finding the shortest path in a graph.
(e) Define a new class SearchGraph extending the Graph class with methods for search- ing the shortest path. Which design pattern(s) can you use in the example?
(f) Define new search algorithms to find the cheapest and fastest paths.
(g) Find the cheapest and fastest paths between Berlin and Cologne.
This exercise sheet is based on the exercises written by Bartosz Telenczuk, Niko Wilbert for the Advanced Scientific Programming in Python School 2011