TP: Geometric drawings: using double loops ♣

Contents

TP: Geometric drawings: using double loops ♣#

Attention

This exercise is marked with a ♣ and is therefore of a more advanced level; do not hesitate to skip it if it seems difficult to you in order to not waste too much time, and to come back to it later.

Exercise#

  • Write a function carre_plein that, for a given non-zero positive integer \(L\), displays a solid square whose sides are of length \(L\) characters on the standard output. For example, for \(L=5\), the function will display:

    *****
    *****
    *****
    *****
    *****
    

Note: Line breaks in Python

By default, the print() function in Python automatically adds a newline.

To avoid this, add the argument end="" to the function:

print("Hello world!", end=""). To skip a line, simply call the function

without any arguments: print().

def carre_plein( L) :
    ### BEGIN SOLUTION
    caractere = "*"
    for i in range(L):
        for j in range(L):
            print(caractere,end="")
        print()
    ### END SOLUTION
carre_plein(5)
*****
*****
*****
*****
*****
carre_plein(3)
***
***
***
  • Same question, but this time the function displays an empty square. For the previous example, this gives:```






```{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
### 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
carre_vide(5)
*****
*   *
*   *
*   *
*****
carre_vide(2)
**
**
carre_vide(1)
*
carre_vide(0)
  • Transform your function so that it is easy to replace the character * with another character caractere:

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
carre_vide(4, "#")
####
#  #
#  #
####
  • Write a function that, for a given positive integer \(h\), displays the upward-pointing isosceles triangle whose outline is described by asterisks, of height \(h\) and base \(2h-1\). For example, for \(h=5\), the function will display:``` * * * * *

    • *
      




```{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
### 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

You will note below that a default value has been given for caractere: *. Therefore, we can use the following two forms:

triangle(4)
   *   
  * *  
 *   * 
*******
triangle(5, "#")
    #    
   # #   
  #   #  
 #     # 
#########
triangle(2)
 * 
***
triangle(1)
*
triangle(0)
  • Write a function that displays a diamond. For example, for \(h=5\), the function will display:``` *

  • *
    
  •   *
    
  • *
    
*



```{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
### 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
losange(5)
    *    
   * *   
  *   *  
 *     * 
*       *
 *     * 
  *   *  
   * *   
    *    
losange(1)
*
losange(0)