From 295a7741a4a1d3d072499438b4d39cac4611480e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20de=20Favereau?=
 <jerome.defavereau@uclouvain.be>
Date: Mon, 12 Oct 2020 09:12:45 +0200
Subject: [PATCH] replace ex4.py

---
 Chapter_4/ex4.py | 58 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 52 insertions(+), 6 deletions(-)

diff --git a/Chapter_4/ex4.py b/Chapter_4/ex4.py
index 912d10f..673ecfc 100644
--- a/Chapter_4/ex4.py
+++ b/Chapter_4/ex4.py
@@ -1,10 +1,56 @@
-import timeit
+months = [
+        {"days": 31, "name": "janvier"},
+        {"days": 29, "name": "fevrier"},
+        {"days": 31, "name": "mars"},
+        {"days": 30, "name": "avril"},
+        {"days": 31, "name": "mai"},
+        {"days": 30, "name": "juin"},
+        {"days": 31, "name": "juillet"},
+        {"days": 31, "name": "aout"},
+        {"days": 30, "name": "septembre"},
+        {"days": 31, "name": "octobre"},
+        {"days": 30, "name": "novembre"},
+        {"days": 31, "name": "decembre"}
+        ]
 
-def multiplication(x,y):
-    if y < 2:
-        return x
+days = [
+        "lundi",
+        "mardi",
+        "mercredi",
+        "jeudi",
+        "vendredi",
+        "samedi",
+        "dimanche"
+        ]
+
+def french_date(day, month):
+    days_in_year = 0
+    for imonth, data in enumerate(months):
+        if imonth+1 < month:
+            days_in_year += data["days"]
+        else:
+            break
+
+    days_in_year += day
+    weekday = days[(days_in_year + 2 - 1) % 7]
+    return f"{weekday} {day} {months[month-1]['name']} 2020"
+
+def date_format(date, dformat = "dd/mm"):
+    if dformat == "dd/mm":
+        day = int(date[:2])
+        month = int(date[3:])
+    elif dformat == "mm/dd":
+        day = int(date[3:])
+        month = int(date[:2])
     else:
-        return x+multiplication(x,y-1)
+        print(f"Format non supporte {dformat}")
+        return ""
 
+    if month > 12 or month < 1:
+        print(f"Mois non valide: {month}")
+        return ""
+    if day > months[month-1]["days"]:
+        print(f"Jour non valide: {day} pour le mois de {months[month-1]['name']}")
+        return ""
 
-print(multiplication(6,7))
+    return french_date(day, month)
-- 
GitLab