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 damier

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

La fonction prend en argument:

Elle construit et retourne alors un tableau carré de taille size x size, qui est rempli comme un damier avec des entiers 0 et 1; la valeur de la cellule d’indice 0 x 0 est correspond au paramètre corner_0_0.

On rappelle par ailleurs que False == 0 et True == 1.

import numpy as np

from corrections.exo_checkers import exo_checkers

# voici deux exemples pour la fonction checkers
exo_checkers.example()
Loading...
# à vous de jouer
def checkers(size, corner_0_0=True):
    return "votre code"
# pour corriger votre code
exo_checkers.correction(checkers)
Loading...

Visualisation

import matplotlib.pyplot as plt
%matplotlib inline
plt.ion();

L’exercice est terminé, mais si vous avez réussi et que vous voulez visualisez le résultat, voici comment vous pouvez aussi voir ce type de tableau :

checkerboard = checkers(8, False)

Pour le voir comme une image:

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

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