Skip to content

Commit bb0b734

Browse files
Initial commit
0 parents  commit bb0b734

10 files changed

+379
-0
lines changed

.github/classroom/autograding.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "Test",
5+
"setup": "sudo -H pip3 install pytest numpy",
6+
"run": "pytest",
7+
"input": "",
8+
"output": "",
9+
"comparison": "included",
10+
"timeout": 10,
11+
"points": null
12+
}
13+
]
14+
}

.github/workflows/classroom.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: GitHub Classroom Workflow
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
name: Autograding
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: education/autograding@v1

.gitignore

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/
130+
131+
132+
.vscode/
133+
.idea/
134+
.pytest_cache/
135+
.directory
136+
.DS_Store

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Homework 4 - Basic NumPy
2+
3+
The deadline of this homework is on **Tuesday, 16th of May, 23:59:00 UTC+2**.
4+
5+
In this homework you should implement three different functions in their respective files.
6+
7+
8+
### 1 Strange Pattern
9+
10+
You come across this strange pattern.
11+
12+
| x | | | x | | | x | |
13+
| ----- | ----- | ----- | ----- | ----- | ----- | ----- | ----- |
14+
| | | **x** | | | **x** | | |
15+
| | **x** | | | **x** | | | **x** |
16+
| **x** | | | **x** | | | **x** | |
17+
| | | **x** | | | **x** | | |
18+
| | **x** | | | **x** | | | **x** |
19+
20+
Mesmerized, you decide you must write a function to generate arbitrary sizes of it. (_Write a function `strange_pattern` that takes a shape tuple `(n, m)` as input and generates a boolean (True for x's and False for blank spaces) 2D NumPy array of the given shape with this pattern_)
21+
22+
**Hint:** Perhaps this strange symbol might help? `::`
23+
24+
### 2 Gaussian analysis
25+
26+
Write a function `gaussian_analysis` which takes four parameters `loc`, `scale`, `lower_bound` and `upper_bound` and returns a tuple of two values (`mean`, `std`).
27+
First of all the function should make sure that `loc`, `scale`, `lower_bound` and `upper_bound` are integers or floats and that `lower_bound` is smaller than `upper_bound` and should return meaningful error messages if those are not the case.
28+
In the function 100 samples of a gaussian distribution should be drawn in respect to the given `loc` and `scale` parameters. Check out the Numpy documentation to find out which function you could use here.
29+
Next, the values below the `lower_bound` and above the `upper_bound` should be filtered out. Afterwards you should calculate the `mean` and the `std`(standard deviation) of the array and return them in a tuple.
30+
31+
32+
33+
### 3. Combination of arrays
34+
35+
Write a function `combination` that takes in two numpy arrays and an optional parameter `axis` which should be 0 by default.
36+
Remove unnecessary dimensions of the input arrays, check whether they can be combined along the given axis and return the combined array.
37+
If the combination is not possible, raise a meaningful error message.
38+
39+
Good luck!

combination.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
3+
# implement your function to combine two numpy arrays
4+
5+
def combination():
6+
# delete the NotImplementedError when you write your function.
7+
raise NotImplementedError
8+
9+
10+
if __name__ == "__main__":
11+
# use this for your own testing!
12+
13+
pass

gaussian_analysis.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import numpy as np
2+
3+
# implement the function gaussian_analysis
4+
5+
def gaussian_analysis():
6+
7+
# delete the NotImplementedError when you write your function.
8+
raise NotImplementedError
9+
10+
11+
if __name__ == "__main__":
12+
# use this for your own testing!
13+
14+
pass

strange_pattern.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
3+
# implement the function strange pattern
4+
5+
def strange_pattern():
6+
# delete the NotImplementedError when you write your function.
7+
raise NotImplementedError
8+
9+
10+
if __name__ == "__main__":
11+
# use this for your own testing!
12+
13+
pass

test_combination.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from hashlib import sha1
2+
import numpy as np
3+
import types
4+
5+
try:
6+
import combination as testfile
7+
except ModuleNotFoundError:
8+
assert False, "The name of your file is suppoesed to be 'combination.py'!"
9+
10+
def imports_of_your_file(filename):
11+
""" Yields all imports in the testfile. """
12+
13+
for name, val in vars(testfile).items():
14+
if isinstance(val, types.ModuleType):
15+
# get direct imports
16+
yield val.__name__
17+
18+
else:
19+
# get from x import y imports
20+
imprt = getattr(testfile, name)
21+
22+
if hasattr(imprt, "__module__") and not str(imprt.__module__).startswith("_") and not str(imprt.__module__) == filename:
23+
yield imprt.__module__
24+
25+
26+
def test_imports(filename="combination", allowed_imports={"numpy"}):
27+
""" Checks if any non-allowed imports have been done. """
28+
29+
assert set(imports_of_your_file(
30+
filename)) <= allowed_imports, "You are not allowed to import any modules except NumPy!"
31+
32+
33+
def test_combination():
34+
a = np.array([[[[1, 2], [3, 4], [5, 6]]]])
35+
b = np.ones((2,2))
36+
37+
result = testfile.combination(a,b)
38+
39+
assert sha1(result).hexdigest(
40+
) == '5709b4ea8b49ac48d42ae3e0bd1b3fb0dbb3249b', 'Your function does not return the right result!'
41+
42+

test_gaussian_analysis.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from hashlib import sha1
2+
3+
import numpy as np
4+
import types
5+
6+
try:
7+
import gaussian_analysis as testfile
8+
except ModuleNotFoundError:
9+
assert False, "The name of your file is suppoesed to be 'gaussian_analysis.py'!"
10+
11+
def imports_of_your_file(filename):
12+
""" Yields all imports in the testfile. """
13+
14+
for name, val in vars(testfile).items():
15+
if isinstance(val, types.ModuleType):
16+
# get direct imports
17+
yield val.__name__
18+
19+
else:
20+
# get from x import y imports
21+
imprt = getattr(testfile, name)
22+
23+
if hasattr(imprt, "__module__") and not str(imprt.__module__).startswith("_") and not str(imprt.__module__) == filename:
24+
yield imprt.__module__
25+
26+
27+
def test_imports(filename="gaussian_analysis", allowed_imports={"numpy"}):
28+
""" Checks if any non-allowed imports have been done. """
29+
30+
assert set(imports_of_your_file(
31+
filename)) <= allowed_imports, "You are not allowed to import any modules except NumPy!"
32+
33+
def test_gaussian_analysis():
34+
loc = 0
35+
scale = 3
36+
lower_bound = 1
37+
upper_bound = 3
38+
39+
result = testfile.gaussian_analysis(loc,scale,lower_bound,upper_bound)
40+
assert type(result) is tuple, 'Your function does not return the right form of result!'
41+
assert result[0] < upper_bound and result[0] > lower_bound, 'Your function does not return the right result!'
42+
43+
44+
45+

test_strange_pattern.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from hashlib import sha1
2+
3+
import numpy as np
4+
import types
5+
6+
try:
7+
import strange_pattern as testfile
8+
except ModuleNotFoundError:
9+
assert False, "The name of your file is suppoesed to be 'strange_pattern.py'!"
10+
11+
def imports_of_your_file(filename):
12+
""" Yields all imports in the testfile. """
13+
14+
for name, val in vars(testfile).items():
15+
if isinstance(val, types.ModuleType):
16+
# get direct imports
17+
yield val.__name__
18+
19+
else:
20+
# get from x import y imports
21+
imprt = getattr(testfile, name)
22+
23+
if hasattr(imprt, "__module__") and not str(imprt.__module__).startswith("_") and not str(imprt.__module__) == filename:
24+
yield imprt.__module__
25+
26+
27+
def test_imports(filename="strange_pattern", allowed_imports={"numpy"}):
28+
""" Checks if any non-allowed imports have been done. """
29+
30+
assert set(imports_of_your_file(
31+
filename)) <= allowed_imports, "You are not allowed to import any modules except NumPy!"
32+
33+
34+
def test_strange_pattern():
35+
result = testfile.strange_pattern((10, 10))
36+
37+
assert type(
38+
result) is np.ndarray, "Your function does not return a NumPy array!"
39+
assert result.dtype is np.dtype(
40+
"bool"), "Your function does not return a boolean array!"
41+
assert sha1(result).hexdigest(
42+
) == '7e69ac17197f17ebccf456b6dfbbe95fe938b7d9', "Your function does not produce the correct pattern!"
43+
44+
result = testfile.strange_pattern((2, 2))
45+
46+
assert sha1(result).hexdigest(
47+
) == "3c585604e87f855973731fea83e21fab9392d2fc", "Your function does not produce the correct pattern in an edge case!"
48+
49+
result = testfile.strange_pattern((0, 0))
50+
51+
assert result.shape == (
52+
0, 0), "Your function does not produce the correct pattern in an edge case!"

0 commit comments

Comments
 (0)