From abd7628ffe28d2d2e9fac130d9e8383a839bf083 Mon Sep 17 00:00:00 2001 From: Tipragot Date: Thu, 21 Sep 2023 15:41:09 +0200 Subject: [PATCH] exo B --- Chapitre 2 - Récursivité/exercices.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Chapitre 2 - Récursivité/exercices.py b/Chapitre 2 - Récursivité/exercices.py index 1a5c342..a8a6d96 100644 --- a/Chapitre 2 - Récursivité/exercices.py +++ b/Chapitre 2 - Récursivité/exercices.py @@ -15,3 +15,20 @@ def compte(t, n=1): print() compte(5) + + +def sommeDesCarres(n): + if n == 1: + return 1 + return n**2 + sommeDesCarres(n-1) + +def produit(liste): + if len(liste) == 1: + return liste[0] + return liste[0] * produit(liste[1:]) + +def sommeDesPositifs(liste): + if len(liste) == 0: + return 0 + value = liste[0] if liste[0] > 0 else 0 + return value + sommeDesPositifs(liste[1:]) \ No newline at end of file