Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Antoinegey solution #17

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: build
name: Assignment Validation

on:
push:

pull_request:

create:
branches:
- 'main'
tags:
- '**'

jobs:
build:
pull_request:

jobs:
test:
name: Test Code
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
Expand All @@ -28,15 +23,28 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pydocstyle
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with pytest
run: pytest -v
lint:
name: Check code style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
pip install flake8 pydocstyle
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=80 --statistics
pydocstyle
- name: Test with pytest
run: |
pytest
flake8 . --count --max-complexity=10 --max-line-length=80 --statistics
- name: Check doc style with pydocstyle
run: pydocstyle

15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Assignment 1 for the DataCamp course X-DataScience Master
# Assignment 1 for the DataCamp course X-DataScience Master - numpy

## What we want you to learn by doing this assignment:

Expand All @@ -10,11 +10,16 @@

## How?

- For the repository by clicking on the `Fork` button on the upper right corner
- Clone the repository of your fork with: `git clone https://github.com/MYLOGIN/datacamp-assignment1` (replace MYLOGIN with your GitHub login)
- Create a branch called `myassignment` using `git checkout -b myassignment`
- Fork the repository by clicking on the `Fork` button on the upper right corner
- Clone the repository of your fork with: `git clone https://github.com/MYLOGIN/datacamp-assignment-numpy` (replace MYLOGIN with your GitHub login)
- Create a branch called `my_user_name` using `git checkout -b my_user_name`
- Make the changes to complete the assignment. You have to modify the files that contain `questions` in their name. Do not modify the files that start with `test_`.
- Open the pull request on GitHub
- Check locally that your solution meet the test by running `pytest` from the root of the repo. You may need to install `pytest` using `pip` or `conda`.
- Check the code formating for your solution using `flake8`. You may need to install `flake8` using `pip` or `conda`.
- Open the pull request on GitHub:
- Create a commit with `git add -u` and `git commit -m "UP my solution"`
- Push your branch on your fork: `git push -u origin my_user_name`
- Go to your repo in your browser and click the `Open a PR` button.
- Keep pushing to your branch until the continuous integration system is green.
- When it is green notify the professors on Slack that your done.

Expand Down
62 changes: 51 additions & 11 deletions numpy_questions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
"""Assignment - using numpy and making a PR.

The goals of this assignment are:
* Use numpy in practice with two easy exercises.
* Use automated tools to validate the code (`pytest` and `flake8`)
* Submit a Pull-Request on github to practice `git`.

The two functions below are skeleton functions. The docstrings explain what
are the inputs, the outputs and the expected error. Fill the function to
complete the assignment. The code should be able to pass the test that we
wrote. To run the tests, use `pytest test_numpy_question.py` at the root of
the repo. It should say that 2 tests ran with success.

We also ask to respect the pep8 convention: https://pep8.org.
This will be enforced with `flake8`. You can check that there is no flake8
errors by calling `flake8` at the root of the repo.
"""
import numpy as np


Expand All @@ -11,22 +28,21 @@ def max_index(X):

Returns
-------
i : int
The row index of the maximum.

j : int
The column index of the maximum.
(i, j) : tuple(int)
The row and columnd index of the maximum.

Raises
------
ValueError
If the input is not a numpy error or
If the input is not a numpy array or
if the shape is not 2D.
"""
i = 0
j = 0

# TODO
if not isinstance(X, np.ndarray) or len(X.shape) != 2:
raise ValueError

i = np.argmax(X) // X.shape[1]
j = np.argmax(X) % X.shape[1]

return i, j

Expand All @@ -37,9 +53,33 @@ def wallis_product(n_terms):
See:
https://en.wikipedia.org/wiki/Wallis_product

XXX : write Parameters and Returns sections as above.
Parameters
----------
n_terms : int
Number of steps in the Wallis product. Note that `n_terms=0` will
consider the product to be `1`.

Returns
-------
pi : float
The approximation of order `n_terms` of pi using the Wallis product.
"""
# XXX : The n_terms is an int that corresponds to the number of
# terms in the product. For example 10000.
return 0.

# Check if the n_terms if correct
if n_terms < 0:
raise ValueError

# Check if n_terms is null
if n_terms == 0:
return 2.0

# Build a range of terms
n = np.arange(n_terms)+1
n_2 = n**2

# Build the intermediate products
product = 4 * n_2 / (4.0 * n_2 - 1)

return 2.0 * np.prod(product)
35 changes: 0 additions & 35 deletions sklearn_questions.py

This file was deleted.

8 changes: 7 additions & 1 deletion test_numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_max_index():

X = np.random.randn(100, 100)
i, j = max_index(X)
assert np.all(X[i, j] <= X)
assert np.all(X[i, j] >= X)

with pytest.raises(ValueError):
max_index(None)
Expand All @@ -29,5 +29,11 @@ def test_max_index():


def test_wallis_product():
pi_approx = wallis_product(0)
assert pi_approx == 2.

pi_approx = wallis_product(1)
assert pi_approx == 8 / 3

pi_approx = wallis_product(100000)
assert abs(pi_approx - m.pi) < 1e-4
31 changes: 0 additions & 31 deletions test_sklearn_questions.py

This file was deleted.