Skip to content

Commit b322b56

Browse files
committed
Réponses ajoutées dans numpy_questions.py
1 parent d31f7ef commit b322b56

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

numpy_questions.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
"""Assignment - using numpy and making a PR.
2-
3-
The goals of this assignment are:
4-
* Use numpy in practice with two easy exercises.
5-
* Use automated tools to validate the code (`pytest` and `flake8`)
6-
* Submit a Pull-Request on github to practice `git`.
7-
8-
The two functions below are skeleton functions. The docstrings explain what
9-
are the inputs, the outputs and the expected error. Fill the function to
10-
complete the assignment. The code should be able to pass the test that we
11-
wrote. To run the tests, use `pytest test_numpy_question.py` at the root of
12-
the repo. It should say that 2 tests ran with success.
13-
14-
We also ask to respect the pep8 convention: https://pep8.org.
15-
This will be enforced with `flake8`. You can check that there is no flake8
16-
errors by calling `flake8` at the root of the repo.
17-
"""
181
import numpy as np
192

203

@@ -29,19 +12,22 @@ def max_index(X):
2912
Returns
3013
-------
3114
(i, j) : tuple(int)
32-
The row and columnd index of the maximum.
15+
The row and column index of the maximum.
3316
3417
Raises
3518
------
3619
ValueError
3720
If the input is not a numpy array or
3821
if the shape is not 2D.
3922
"""
40-
i = 0
41-
j = 0
23+
if not isinstance(X, np.ndarray):
24+
raise ValueError("Input must be a numpy array.")
4225

43-
# TODO
26+
if X.ndim != 2:
27+
raise ValueError("Input array must be 2D.")
4428

29+
max_idx = np.argmax(X)
30+
i, j = np.unravel_index(max_idx, X.shape)
4531
return i, j
4632

4733

@@ -62,6 +48,10 @@ def wallis_product(n_terms):
6248
pi : float
6349
The approximation of order `n_terms` of pi using the Wallis product.
6450
"""
65-
# XXX : The n_terms is an int that corresponds to the number of
66-
# terms in the product. For example 10000.
67-
return 0.
51+
if n_terms == 0:
52+
return 2.0
53+
54+
k = np.arange(1, n_terms + 1)
55+
product = np.prod((4.0 * k * k) / (4.0 * k * k - 1.0))
56+
pi_approx = 2.0 * product
57+
return pi_approx

0 commit comments

Comments
 (0)