---
jupytext:
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

+++ {"nbgrader": {"grade": false, "grade_id": "cell-b31ba63729e95016", "locked": true, "schema_version": 3, "solution": false, "task": false}}

# TP : dessins géométriques : utilisation de doubles boucles ♣

+++

:::{attention}
Cet exercice est marqué d'un ♣ et est donc d'un niveau plus avancé ; n'hésitez pas à le
passer s'il vous paraît difficile afin de ne pas perdre trop de temps, et à revenir
dessus ultérieurement.
:::

+++ {"nbgrader": {"grade": false, "grade_id": "cell-8e4fbe3af67bde53", "locked": true, "schema_version": 3, "solution": false, "task": false}}

## Exercice

+++ {"nbgrader": {"grade": false, "grade_id": "cell-352f77c979379bf6", "locked": true, "schema_version": 3, "solution": false, "task": false}}

- Écrivez une fonction `carre_plein` qui, pour un entier positif non nul $L$ donné,
  affiche sur la sortie standard un carré plein dont les côtés sont de longueur $L$
  caractères. Par exemple, pour $L=5$, la fonction affichera :

  ```
  *****
  *****
  *****
  *****
  *****
  ```

+++ {"nbgrader": {"grade": false, "grade_id": "cell-6c0789c02fcf5837", "locked": true, "schema_version": 3, "solution": false, "task": false}}

:::{admonition} Indication : Les sauts de ligne en python
Par défaut, la fonction `print()` en python ajoute automatiquement un retour à la ligne.
Pour l'éviter, on rajoute l'argument `end=""` à la fonction :
`print("Hello world!", end="")`. Pour sauter une ligne, il suffit d'appeler la fonction
sans argument : `print()`.
:::

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-aa60e6ba0f4ca604
  locked: false
  schema_version: 3
  solution: true
  task: false
---
def carre_plein( L) :
    ### BEGIN SOLUTION
    caractere = "*"
    for i in range(L):
        for j in range(L):
            print(caractere,end="")
        print()
    ### END SOLUTION
```

```{code-cell} ipython3
carre_plein(5)
```

```{code-cell} ipython3
carre_plein(3)
```

+++ {"nbgrader": {"grade": false, "grade_id": "cell-38495cd34c8d0e2c", "locked": true, "schema_version": 3, "solution": false, "task": false}}

- Même question mais cette fois la fonction affiche un carré vide. Pour l’exemple
  précédent, cela donne :

  ```
  *****
  *   *
  *   *
  *   *
  *****
  ```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-e7cca0ee7af7856b
  locked: false
  schema_version: 3
  solution: true
  task: false
---
def carre_vide(L):
    ### BEGIN SOLUTION
    if ( L >= 1 ):
        for j in range(L):
            print("*", end="")
    print()
    for i in range(1, L-1):
        print("*", end="")
        for j in range(L-2):
            print(" ", end="")
        print("*")
    if ( L >= 2 ):
        for j in range(L):
            print("*", end="")
    print()
    ### END SOLUTION
```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-5a8c9d90a10cbccf
  locked: false
  schema_version: 3
  solution: true
  task: false
---
### BEGIN SOLUTION

## Solution alternative
def carre_vide(L) :
    for i in range(L):
        for j in range(L):
            if ( i == 0 or i == L-1 or j == 0 or j == L-1):
                print("*", end="")
            else:
                print(" ", end="")
        
        print()

### END SOLUTION
```

```{code-cell} ipython3
carre_vide(5)
```

```{code-cell} ipython3
carre_vide(2)
```

```{code-cell} ipython3
carre_vide(1)
```

```{code-cell} ipython3
carre_vide(0)
```

+++ {"nbgrader": {"grade": false, "grade_id": "cell-21ee6e6db6c72122", "locked": true, "schema_version": 3, "solution": false, "task": false}}

- Transformez votre fonction pour qu’il soit simple de remplacer le caractère `*` par un
  autre caractère `caractere` :

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-432dd624ee8d341f
  locked: false
  schema_version: 3
  solution: true
  task: false
---
def carre_vide(L, caractere):
    ### BEGIN SOLUTION
    if ( L >= 1 ):
        for j in range(L):
            print(caractere, end="")
    print()
    for i in range(1, L-1):
        print(caractere, end="")
        for j in range(L-2):
            print(" ", end="")
        print(caractere)
    if ( L >= 2 ):
        for j in range(L):
            print(caractere, end="")
    print()
    ### END SOLUTION
```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-fd7d28c3addf4b85
  locked: true
  schema_version: 3
  solution: false
  task: false
---
carre_vide(4, "#")
```

+++ {"nbgrader": {"grade": false, "grade_id": "cell-d2be9b92b87f6618", "locked": true, "schema_version": 3, "solution": false, "task": false}}

- Écrivez une fonction qui, pour un entier positif $h$ donné, affiche le triangle isocèle
  pointe en haut  dont le contour est décrit par des étoiles, de hauteur $h$ et de base
  $2h-1$. Par exemple, pour $h=5$, la fonction affichera :
  ```
          *
         * *
        *   *
       *     *
      *********
  ```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-ed285270a4f27c05
  locked: false
  schema_version: 3
  solution: true
  task: false
---
def triangle(h, caractere="*"): #dans cette fonction, caractere a la valeur "*" par défaut :
                                #si la fonction est appelée sans l’argument, il prend cette valeur.
    ### BEGIN SOLUTION
    for i in range(h-1):
        for j in range(h-1-i):
            print(" ",  end="")
        print(caractere,  end="")
        if (i > 0):
            for j in range(2*i-1):
                print(" ",  end="")
            print(caractere,  end="")
        print()
    for j in range(2*h-1): 
        print(caractere,  end="")
    print()
    ### END SOLUTION
```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-9f1f87379aa3a076
  locked: false
  schema_version: 3
  solution: true
  task: false
---
### BEGIN SOLUTION

#Solution alternative
def triangle(h, caractere="*"):
    for i in range(h):
        for j in range(2*h-1):
            if ( i == -j + h - 1  or  i == h-1  or  i == j - h + 1 ):
                print(caractere,  end="")
            else:
                print(" ",  end="")
        print()
### END SOLUTION
```

+++ {"nbgrader": {"grade": false, "grade_id": "cell-c684cbb420e12f44", "locked": true, "schema_version": 3, "solution": false, "task": false}}

Vous noterez ci-dessous que l'on a donné une valeur par défaut pour `caractere` : `*`. Du
coup, on peut utiliser les deux formes suivantes :

```{code-cell} ipython3
triangle(4)
```

```{code-cell} ipython3
triangle(5, "#")
```

```{code-cell} ipython3
triangle(2)
```

```{code-cell} ipython3
triangle(1)
```

```{code-cell} ipython3
triangle(0)
```

+++ {"nbgrader": {"grade": false, "grade_id": "cell-21a24a581169dd5f", "locked": true, "schema_version": 3, "solution": false, "task": false}}

- Écrivez une fonction qui affiche un losange. Par exemple, pour $h=5$, la fonction
  affichera :

  ```
      *
     * *
    *   *
   *     *
  *       *
   *     *
    *   *
     * *
      *
  ```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-b40856987b98d13d
  locked: false
  schema_version: 3
  solution: true
  task: false
---
def losange(h, caractere="*"):
    ### BEGIN SOLUTION
    for i in range(0, h):
        for j in range(0, h-i-1):
            print(" ",  end="")
        print(caractere,  end="")
        if (i > 0):
            for j in range(2*i-1):
                print(" ",  end="")
            print(caractere,  end="")
        print()
    for i in range(h-2, -1, -1):
        for j in range(h-i-1):
            print(" ", end="")
        print(caractere,  end="")
        if (i > 0):
            for j in range(2*i-1):
                print(" ",  end="")
            print(caractere,  end="")
        print()
    ### END SOLUTION
```

```{code-cell} ipython3
---
nbgrader:
  grade: false
  grade_id: cell-91a47942d015cc05
  locked: false
  schema_version: 3
  solution: true
  task: false
---
### BEGIN SOLUTION
## Solution alternative
def losange( h, caractere="*"):
    for i in range(2*h-1):
        for j in range(2*h-1):
            if ( ( i <= h-1 and (i == -j +   h - 1 or i == j - h + 1) ) or
                 ( i >  h-1 and (i == -j + 3*h - 3 or i == j + h - 1) ) ):
                print(caractere, end="")
            else :
                print(" ",  end="")
        print()
### END SOLUTION
```

```{code-cell} ipython3
losange(5)
```

```{code-cell} ipython3
losange(1)
```

```{code-cell} ipython3
losange(0)
```
