Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Licence CC BY-NC-ND Thierry Parmentelat & Arnaud Legout Inria - UCA

construire un tableau en escalier

On vous demande d’écrire une fonction stairs qui crée un tableau numpy.

La fonction prend en argument un entier taille et construit un tableau carré de taille 2taille+12*taille+1.

Aux quatre coins du tableau on trouve la valeur 0. Dans la case centrale on trouve la valeur 2taille2*taille.

Si vous partez de n’importe quelle case et que vous vous déplacez d’une case horizontalement ou verticalement vers une cas plus proche du centre, vous incrémentez la valeur du tableau de 1.

import numpy as np

from corrections.exo_stairs import exo_stairs

# voici deux exemples pour la fonction stairs
exo_stairs.example()
Loading...
# à vous de jouer
def stairs(taille):
    return "votre code"
# pour corriger votre code
exo_stairs.correction(stairs)
Loading...

Visualisation

import matplotlib.pyplot as plt
%matplotlib inline
plt.ion()
<contextlib.ExitStack at 0x7f9e009db0e0>

L’exercice est terminé, voyons à nouveau notre résultat sous forme d’image :

squares = stairs(100)
The history saving thread hit an unexpected error (OperationalError('database is locked')).History will not be written to the database.

Pour le voir comme une image avec un niveau de gris comme code de couleurs (noir = 0, blanc = maximum = 201 dans notre cas) :

# convertir en flottant pour imshow
squares = squares.astype(float)
# afficher avec une colormap 'gray'
plt.imshow(squares, cmap='gray');
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 2
      1 # convertir en flottant pour imshow
----> 2 squares = squares.astype(float)
      3 # afficher avec une colormap 'gray'
      4 plt.imshow(squares, cmap='gray');

AttributeError: 'str' object has no attribute 'astype'