Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Object-oriented Programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
nilsl99
Object-oriented Programming
Commits
05f1dab1
Commit
05f1dab1
authored
2 years ago
by
nilsl99
Browse files
Options
Downloads
Patches
Plain Diff
Added fib5
parent
4d614c78
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exercise05_1.ipynb
+38
-8
38 additions, 8 deletions
exercise05_1.ipynb
with
38 additions
and
8 deletions
exercise05_1.ipynb
+
38
−
8
View file @
05f1dab1
...
...
@@ -77,12 +77,25 @@
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"answers = {0: 1, 1: 1}\n",
"def fib5(n):\n",
" if not n in answers:\n",
" answers[n] = fib5(n-1) + fib5(n-2)\n",
" return answers[n]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"
3.06
s ±
90.7
ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
"
2.55
s ±
52.9
ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
...
...
@@ -92,14 +105,14 @@
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
8
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"
62.2
ns ±
1.44
ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n"
"
57.6
ns ±
0.131
ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n"
]
}
],
...
...
@@ -109,14 +122,14 @@
},
{
"cell_type": "code",
"execution_count":
8
,
"execution_count":
9
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"
4.09
µs ±
193
ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n"
"
3.37
µs ±
82.7
ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n"
]
}
],
...
...
@@ -126,20 +139,37 @@
},
{
"cell_type": "code",
"execution_count":
9
,
"execution_count":
10
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.
76
µs ± 7
4
.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n"
"2.
2
µs ± 7
5
.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n"
]
}
],
"source": [
"%timeit fib4(35)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"103 ns ± 0.943 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n"
]
}
],
"source": [
"%timeit fib5(35)"
]
}
],
"metadata": {
...
...
@@ -158,7 +188,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.
8
"
"version": "3.10.
6
"
},
"orig_nbformat": 4
},
...
...
%% Cell type:markdown id: tags:
## OOP Homework 05
### Exercise 1
Dilay Hamaykaya, Nils Liebreich
%% Cell type:code id: tags:
```
python
from
functools
import
cache
```
%% Cell type:code id: tags:
```
python
def
fib1
(
n
):
if
n
<=
1
:
return
1
else
:
return
fib1
(
n
-
1
)
+
fib1
(
n
-
2
)
```
%% Cell type:code id: tags:
```
python
@cache
def
fib2
(
n
):
if
n
<=
1
:
return
1
else
:
return
fib2
(
n
-
1
)
+
fib2
(
n
-
2
)
```
%% Cell type:code id: tags:
```
python
def
fib3
(
n
):
fibonacci
=
[
1
]
*
(
n
+
1
)
for
i
in
range
(
2
,
n
+
1
):
fibonacci
[
i
]
=
fibonacci
[
i
-
1
]
+
fibonacci
[
i
-
2
]
return
fibonacci
[
n
]
```
%% Cell type:code id: tags:
```
python
def
fib4
(
n
):
a
=
b
=
1
while
n
>
0
:
a
,
b
=
a
+
b
,
a
n
-=
1
return
b
```
%% Cell type:code id: tags:
```
python
answers
=
{
0
:
1
,
1
:
1
}
def
fib5
(
n
):
if
not
n
in
answers
:
answers
[
n
]
=
fib5
(
n
-
1
)
+
fib5
(
n
-
2
)
return
answers
[
n
]
```
%% Cell type:code id: tags:
```
python
%
timeit
fib1
(
35
)
```
%% Output
3.06
s ±
90.7
ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
2.55
s ±
52.9
ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%% Cell type:code id: tags:
```
python
%
timeit
fib2
(
35
)
```
%% Output
62.2
ns ±
1.44
ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
57.6
ns ±
0.131
ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
%% Cell type:code id: tags:
```
python
%
timeit
fib3
(
35
)
```
%% Output
4.09
µs ±
193
ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
3.37
µs ±
82.7
ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
%% Cell type:code id: tags:
```
python
%
timeit
fib4
(
35
)
```
%% Output
2.76 µs ± 74.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
2.2 µs ± 75.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
%% Cell type:code id: tags:
```
python
%
timeit
fib5
(
35
)
```
%% Output
103 ns ± 0.943 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
...
...
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