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
55b6394a
Commit
55b6394a
authored
6 years ago
by
jkvis
Browse files
Options
Downloads
Patches
Plain Diff
Updated for Python 3
parent
084e3e4a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
oop/oop.pdf
+0
-0
0 additions, 0 deletions
oop/oop.pdf
oop/oop.tex
+12
-12
12 additions, 12 deletions
oop/oop.tex
oop/solutions.pdf
+0
-0
0 additions, 0 deletions
oop/solutions.pdf
oop/solutions.tex
+18
-18
18 additions, 18 deletions
oop/solutions.tex
with
30 additions
and
30 deletions
oop/oop.pdf
+
0
−
0
View file @
55b6394a
No preview for this file type
This diff is collapsed.
Click to expand it.
oop/oop.tex
+
12
−
12
View file @
55b6394a
...
@@ -192,7 +192,7 @@ create new instances of a class.
...
@@ -192,7 +192,7 @@ create new instances of a class.
\begin{minted}
{
python
}
\begin{minted}
{
python
}
origin = Vector()
origin = Vector()
v1 = Vector(2, 1)
v1 = Vector(2, 1)
print
v1.x, origin.y
print
(
v1.x, origin.y
)
\end{minted}
\end{minted}
\bigskip
\bigskip
...
@@ -224,7 +224,7 @@ class Vector(object):
...
@@ -224,7 +224,7 @@ class Vector(object):
\item
we agree not to access this attribute directly:
\item
we agree not to access this attribute directly:
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> v1 = Vector(1, 2)
>>> v1 = Vector(1, 2)
>>> print
v1.
_
secret
# this is forbidden
>>> print
(
v1.
_
secret
)
# this is forbidden
\end{minted}
\end{minted}
\end{itemize}
\end{itemize}
...
@@ -254,7 +254,7 @@ origin.distance(v1)
...
@@ -254,7 +254,7 @@ origin.distance(v1)
\begin{frame}
[fragile]
{
Print representation of an object
}
\begin{frame}
[fragile]
{
Print representation of an object
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> v1 = Vector(4, 3)
>>> v1 = Vector(4, 3)
>>> print
v1
>>> print
(
v1
)
<
__
main
__
.Vector object at 0x7f41ab878450>
<
__
main
__
.Vector object at 0x7f41ab878450>
\end{minted}
\end{minted}
...
@@ -267,7 +267,7 @@ origin.distance(v1)
...
@@ -267,7 +267,7 @@ origin.distance(v1)
when using the
\mintinline
{
python
}{
print
}
function;
when using the
\mintinline
{
python
}{
print
}
function;
\item
we are in control of what is printed, e.g., for the vector class:
\item
we are in control of what is printed, e.g., for the vector class:
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
v1
>>> print
(
v1
)
<4, 3>
<4, 3>
\end{minted}
\end{minted}
\end{itemize}
\end{itemize}
...
@@ -297,7 +297,7 @@ class Vector(object):
...
@@ -297,7 +297,7 @@ class Vector(object):
\item
get the type of an instance:
\item
get the type of an instance:
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> v1 = Vector(4, 3)
>>> v1 = Vector(4, 3)
>>> print
type(v1)
>>> print
(
type(v1)
)
\end{minted}
\end{minted}
%% start minted fix
%% start minted fix
\vspace
{
-.08cm
}
\vspace
{
-.08cm
}
...
@@ -306,20 +306,20 @@ class Vector(object):
...
@@ -306,20 +306,20 @@ class Vector(object):
\item
that also works for the class:
\item
that also works for the class:
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
type(Vector)
>>> print
(
type(Vector)
)
<type 'type'>
<type 'type'>
\end{minted}
\end{minted}
\item
use
\mintinline
{
python
}{
isinstance()
}
to check if an object is a
\item
use
\mintinline
{
python
}{
isinstance()
}
to check if an object is a
vector:
vector:
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
isinstance(v1, Vector)
>>> print
(
isinstance(v1, Vector)
)
True
True
\end{minted}
\end{minted}
\item
what happens here:
\item
what happens here:
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
v1.distance(4)
>>> print
(
v1.distance(4)
)
\end{minted}
\end{minted}
\end{itemize}
\end{itemize}
\end{frame}
\end{frame}
...
@@ -328,7 +328,7 @@ True
...
@@ -328,7 +328,7 @@ True
\begin{itemize}
\begin{itemize}
\item
define special operators like:
\item
define special operators like:
\mintinline
{
python
}{
+,-,==,<,>,len(),...
}
\mintinline
{
python
}{
+,-,==,<,>,len(),...
}
see:
\
path
{
https://docs.python.org/
2
/reference/datamodel.html#basic-customization
}
see:
\
url
{
https://docs.python.org/
3
/reference/datamodel.html#basic-customization
}
\item
these can be
\textcolor
{
pms280
_
compl
}{
overloaded
}
to work with your
\item
these can be
\textcolor
{
pms280
_
compl
}{
overloaded
}
to work with your
class (keep it sensible);
class (keep it sensible);
...
@@ -357,7 +357,7 @@ class Vector(object):
...
@@ -357,7 +357,7 @@ class Vector(object):
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> v1 = Vector(1, -6)
>>> v1 = Vector(1, -6)
>>> v2 = Vector(3, 4.5)
>>> v2 = Vector(3, 4.5)
>>> print
v1 + v2
>>> print
(
v1 + v2
)
<4, -1.5>
<4, -1.5>
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -415,8 +415,8 @@ containing two integers: \mintinline{python}{numerator} and
...
@@ -415,8 +415,8 @@ containing two integers: \mintinline{python}{numerator} and
\bigskip
\bigskip
see:
\
path
{
https://github.com/lumc-python/oop
}
\\
see:
\
url
{
https://github.com/lumc-python/oop
}
\\
or go directly to:
\textcolor
{
pms280
_
compl
}{
\
path
{
https://classroom.github.com/a/8BnbL9fD
}}
or go directly to:
\textcolor
{
pms280
_
compl
}{
\
url
{
https://classroom.github.com/a/8BnbL9fD
}}
\end{frame}
\end{frame}
\end{document}
\end{document}
This diff is collapsed.
Click to expand it.
oop/solutions.pdf
+
0
−
0
View file @
55b6394a
No preview for this file type
This diff is collapsed.
Click to expand it.
oop/solutions.tex
+
18
−
18
View file @
55b6394a
...
@@ -28,7 +28,7 @@
...
@@ -28,7 +28,7 @@
\author
{
Jonathan~K.~Vis
}
\author
{
Jonathan~K.~Vis
}
\institute
[LUMC]
{
Dept. of Human Genetics, Leiden University Medical Center
}
\institute
[LUMC]
{
Dept. of Human Genetics, Leiden University Medical Center
}
\date
{
September 21st, 2017
}
\date
{}
\title
{
Solutions --- Object Oriented Programming
}
\title
{
Solutions --- Object Oriented Programming
}
\begin{document}
\begin{document}
...
@@ -75,7 +75,7 @@ class Fraction(object):
...
@@ -75,7 +75,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2)
>>> print
(
Fraction(1, 2)
)
1/2
1/2
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -102,7 +102,7 @@ class Fraction(object):
...
@@ -102,7 +102,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2) + Fraction(2, 3)
>>> print
(
Fraction(1, 2) + Fraction(2, 3)
)
7/6
7/6
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -130,7 +130,7 @@ class Fraction(object):
...
@@ -130,7 +130,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2).invert()
>>> print
(
Fraction(1, 2).invert()
)
2/1
2/1
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -151,14 +151,14 @@ class Fraction(object):
...
@@ -151,14 +151,14 @@ class Fraction(object):
return Fraction(self.
_
denominator, self.
_
numerator)
return Fraction(self.
_
denominator, self.
_
numerator)
def to
_
float(self):
def to
_
float(self):
return
float(
self.
_
numerator
)
/
float(
self.
_
denominator
)
return self.
_
numerator / self.
_
denominator
\end{minted}
\end{minted}
\vfill
\vfill
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2).to
_
float()
>>> print
(
Fraction(1, 2).to
_
float()
)
0.5
0.5
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -174,17 +174,17 @@ class Fraction(object):
...
@@ -174,17 +174,17 @@ class Fraction(object):
return Fraction(self.
_
denominator, self.
_
numerator)
return Fraction(self.
_
denominator, self.
_
numerator)
def to
_
float(self):
def to
_
float(self):
return
float(
self.
_
numerator
)
/
float(
self.
_
denominator
)
return self.
_
numerator / self.
_
denominator
def integer(self):
def integer(self):
return
int(
self.
_
numerator
) / int(
self.
_
denominator
)
return self.
_
numerator
//
self.
_
denominator
\end{minted}
\end{minted}
\vfill
\vfill
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(7, 6).integer()
>>> print
(
Fraction(7, 6).integer()
)
1
1
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -197,10 +197,10 @@ class Fraction(object):
...
@@ -197,10 +197,10 @@ class Fraction(object):
...
...
def to
_
float(self):
def to
_
float(self):
return
float(
self.
_
numerator
)
/
float(
self.
_
denominator
)
return self.
_
numerator / self.
_
denominator
def integer(self):
def integer(self):
return
int(
self.
_
numerator
) / int(
self.
_
denominator
)
return self.
_
numerator
//
self.
_
denominator
def
__
sub
__
(self, other):
def
__
sub
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator -
return Fraction(self.
_
numerator * other.
_
denominator -
...
@@ -212,7 +212,7 @@ class Fraction(object):
...
@@ -212,7 +212,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2) - Fraction(2, 3)
>>> print
(
Fraction(1, 2) - Fraction(2, 3)
)
-1/6
-1/6
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -226,7 +226,7 @@ class Fraction(object):
...
@@ -226,7 +226,7 @@ class Fraction(object):
...
...
def integer(self):
def integer(self):
return
int(
self.
_
numerator
) / int(
self.
_
denominator
)
return self.
_
numerator
//
self.
_
denominator
def
__
sub
__
(self, other):
def
__
sub
__
(self, other):
return Fraction(self.
_
numerator * other.
_
denominator -
return Fraction(self.
_
numerator * other.
_
denominator -
...
@@ -242,7 +242,7 @@ class Fraction(object):
...
@@ -242,7 +242,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2) * Fraction(2, 3)
>>> print
(
Fraction(1, 2) * Fraction(2, 3)
)
2/6
2/6
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -271,7 +271,7 @@ class Fraction(object):
...
@@ -271,7 +271,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(1, 2) / Fraction(2, 3)
>>> print
(
Fraction(1, 2) / Fraction(2, 3)
)
3/4
3/4
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
@@ -290,8 +290,8 @@ class Fraction(object):
...
@@ -290,8 +290,8 @@ class Fraction(object):
def simplify(self):
def simplify(self):
divisor = gcd(self.
_
numerator, self.
_
denominator)
divisor = gcd(self.
_
numerator, self.
_
denominator)
self.
_
numerator = self.
_
numerator / divisor
self.
_
numerator = self.
_
numerator /
/
divisor
self.
_
denominator = self.
_
denominator / divisor
self.
_
denominator = self.
_
denominator /
/
divisor
return self
return self
\end{minted}
\end{minted}
...
@@ -299,7 +299,7 @@ class Fraction(object):
...
@@ -299,7 +299,7 @@ class Fraction(object):
\texttt
{
test
\_
fraction.py
}
\texttt
{
test
\_
fraction.py
}
\begin{minted}
{
python
}
\begin{minted}
{
python
}
>>> print
Fraction(2, 6).simplify()
>>> print
(
Fraction(2, 6).simplify()
)
1/3
1/3
\end{minted}
\end{minted}
\end{frame}
\end{frame}
...
...
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