Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programming course
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
courses
Programming course
Commits
ca828f15
Commit
ca828f15
authored
7 years ago
by
jkvis
Browse files
Options
Downloads
Patches
Plain Diff
Solutions for oop
parent
f4ba19b5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
oop/solutions.pdf
+0
-0
0 additions, 0 deletions
oop/solutions.pdf
oop/solutions.tex
+352
-0
352 additions, 0 deletions
oop/solutions.tex
with
352 additions
and
0 deletions
oop/solutions.pdf
0 → 100644
+
0
−
0
View file @
ca828f15
File added
This diff is collapsed.
Click to expand it.
oop/solutions.tex
0 → 100644
+
352
−
0
View file @
ca828f15
\documentclass
{
beamer
}
\usepackage
{
color
}
\usepackage
{
graphicx
}
\usepackage
{
minted
}
\usepackage
{
url
}
\definecolor
{
pms280
}{
HTML
}{
00247d
}
\definecolor
{
pms280
_
2nd
}{
HTML
}{
385499
}
\definecolor
{
pms280
_
4th
}{
HTML
}{
7185b6
}
\definecolor
{
pms280
_
8th
}{
HTML
}{
e2e6F0
}
\definecolor
{
pms280
_
compl
}{
HTML
}{
7d5800
}
\usetheme
{
Boadilla
}
\useoutertheme
{
infolines
}
\useinnertheme
{
circles
}
\setbeamercolor
{
author in head/foot
}{
bg=pms280,fg=white
}
\setbeamercolor
{
title in head/foot
}{
bg=pms280
_
2nd,fg=white
}
\setbeamercolor
{
date in head/foot
}{
bg=pms280
_
4th,fg=white
}
\setbeamercolor
{
block title
}{
bg=pms280,fg=white
}
\setbeamercolor
{
block body
}{
bg=pms280
_
8th
}
\setbeamercolor
{
title
}{
fg=pms280
}
\setbeamercolor
{
titlelike
}{
fg=pms280
}
\setbeamercolor
{
itemize item
}{
fg=pms280
}
\setbeamercolor
{
itemize subitem
}{
fg=pms280
}
\setbeamertemplate
{
enumerate items
}
[default]
\setbeamercolor
{
enumerate item
}{
fg=pms280
}
\author
{
Jonathan~K.~Vis
}
\institute
[LUMC]
{
Dept. of Human Genetics, Leiden University Medical Center
}
\date
{
September 21st, 2017
}
\title
{
Solutions --- Object Oriented Programming
}
\begin{document}
\beamertemplatenavigationsymbolsempty
\begin{frame}
\titlepage
\vfill
\hfill
\textcolor
{
pms280
_
compl
}{
\texttt
{
j.k.vis@lumc.nl
}}
\end{frame}
\begin{frame}
[fragile]
{
0. Skeleton
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
def
__
init
__
(self, numerator, denominator=1):
self.
_
numerator = numerator
self.
_
denominator = denominator
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> f1 = Fraction(1, 2)
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
1. Adding a print representation
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
def
__
init
__
(self, numerator, denominator=1):
self.
_
numerator = numerator
self.
_
denominator = denominator
def
__
str
__
(self):
return str(self.
_
numerator) + '/' + str(self.
_
denominator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2)
1/2
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
2. Add the
\texttt
{
+
}
operator
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
def
__
init
__
(self, numerator, denominator=1):
self.
_
numerator = numerator
self.
_
denominator = denominator
def
__
str
__
(self):
return str(self.
_
numerator) + '/' + str(self.
_
denominator)
def
__
add
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator +
other.
_
numerator * self.
_
denominator,
self.
_
denominator * other.
_
denominator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2) + Fraction(2, 3)
7/6
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
3. Add an invert function
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
...
def
__
str
__
(self):
return str(self.
_
numerator) + '/' + str(self.
_
denominator)
def
__
add
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator +
other.
_
numerator * self.
_
denominator,
self.
_
denominator * other.
_
denominator)
def invert(self):
return Fraction(self.
_
denominator, self.
_
numerator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2).invert()
2/1
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
4. Conversion to a decimal representation
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
...
def
__
add
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator +
other.
_
numerator * self.
_
denominator,
self.
_
denominator * other.
_
denominator)
def invert(self):
return Fraction(self.
_
denominator, self.
_
numerator)
def to
_
float(self):
return float(self.
_
numerator) / float(self.
_
denominator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2).to
_
float()
0.5
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
5. Get the integer part of a fraction
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
...
def invert(self):
return Fraction(self.
_
denominator, self.
_
numerator)
def to
_
float(self):
return float(self.
_
numerator) / float(self.
_
denominator)
def integer(self):
return int(self.
_
numerator) / int(self.
_
denominator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(7, 6).integer()
1
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
6a. Substracting
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
...
def to
_
float(self):
return float(self.
_
numerator) / float(self.
_
denominator)
def integer(self):
return int(self.
_
numerator) / int(self.
_
denominator)
def
__
sub
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator -
other.
_
numerator * self.
_
denominator,
self.
_
denominator * other.
_
denominator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2) - Fraction(2, 3)
-1/6
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
6b. Multiplication
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
...
def integer(self):
return int(self.
_
numerator) / int(self.
_
denominator)
def
__
sub
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator -
other.
_
numerator * self.
_
denominator,
self.
_
denominator * other.
_
denominator)
def
__
mul
__
(self, other):
return Fraction(self.
_
numerator * other.
_
numerator,
self.
_
denominator * other.
_
denominator)
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2) * Fraction(2, 3)
2/6
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
6c. Division
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
class Fraction(object):
...
def
__
sub
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator -
other.
_
numerator * self.
_
denominator,
self.
_
denominator * other.
_
denominator)
def
__
mul
__
(self, other):
return Fraction(self.
_
numerator * other.
_
numerator,
self.
_
denominator * other.
_
denominator)
def
__
div
__
(self, other):
return self * other.invert()
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(1, 2) / Fraction(2, 3)
3/4
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
7. Simplification
}
\small
\texttt
{
fraction.py
}
\begin{minted}
{
python
}
def gcd(a, b):
if b == 0:
return a
return gcd(b, a
% b)
class Fraction(object):
...
def simplify(self):
divisor = gcd(self.
_
numerator, self.
_
denominator)
self.
_
numerator = self.
_
numerator / divisor
self.
_
denominator = self.
_
denominator / divisor
return self
\end{minted}
\vfill
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
>>> print Fraction(2, 6).simplify()
1/3
\end{minted}
\end{frame}
\begin{frame}
[fragile]
{
8--12. More advanced stuff
}
8. It would be a good idea to
\mintinline
{
python
}{
simplify()
}
after each
arithmetic operator.
\\
\vfill
9. Here, you really need the fractions to be in a
\emph
{
normal
}
form.
\\
\vfill
10. Probably (for printing reasons) you would like for the numerator to
be negative and not the denominator, avoid
\mintinline
{
python
}{
1/-4
}
. And
convert to positive when both the numerator and denominator are negative.
\\
\vfill
11. Something like:
{
\small
\begin{minted}
{
python
}
def
__
mul
__
(self, other):
if isinstance(other, int):
return Fraction(self.
_
numerator * other, self.
_
denominator)
return Fraction(self.
_
numerator * other.
_
numerator,
self.
_
denominator * other.
_
denominator)
\end{minted}
}
\vfill
12. Using
\mintinline
{
python
}{
isinstance(numerator, int)
}
etc. in the
\mintinline
{
python
}{__
init
__}
function.
\end{frame}
\begin{frame}
[fragile]
{
General remarks
}
\begin{itemize}
\item
As always, more docstrings and
\emph
{
more
}
comments;
\item
All
\mintinline
{
python
}{
import
}
s on the top of the file;
\item
\mintinline
{
python
}{
a is b
}
does
\emph
{
not
}
check for numerical
equality: use
\mintinline
{
python
}{
==
}
;
\item
\emph
{
Never
}
use bitwise operators:
\mintinline
{
python
}{&
, |,
^
, ~
}
:
use
\mintinline
{
python
}{
and or not
}
;
\item
Prefer not calling the special functions, e.g.,
\mintinline
{
python
}{__
add
__}
directly: use
\mintinline
{
python
}{
+
}
;
\item
Try to make your code look
\emph
{
beautiful
}
.
\end{itemize}
\end{frame}
\end{document}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment