diff --git a/Chapter_3/ex3.py b/Chapter_3/ex3.py index 845ddf297e0c159d31c7eccb5e000969b3344397..bf99c0da2667a4dc45c12c95f4fd8e68283fe452 100644 --- a/Chapter_3/ex3.py +++ b/Chapter_3/ex3.py @@ -1,4 +1,3 @@ - txt="Les objectifs de Python en font un langage pedagogique ideal.C'est un langage general-purpose:il s'adapte a toutes les applications" seperatorList = " ,.;-'\":\n/\\?+()’!°" @@ -22,3 +21,25 @@ while len(txt) > 0: txt="" print("Il y a {0} mots dans la phrase.".format(n_words)) + +# solution alternative sans utiliser "find" +# moins optimale sur de grands textes + +in_word = False +words = [] +curword = "" + +for letter in txt: + if letter in separators: + if in_word: + words.append(curword) + curword = "" + in_word = False + else: + in_word = True + curword += letter + +if in_word: + words += curword + +print("il y a", len(words), "mot(s)") \ No newline at end of file diff --git a/Chapter_4/ex4.py b/Chapter_4/ex4.py index 912d10f9132497eb3fc5cda655f4d21672032577..673ecfca5a4d3b90f76588e802b22500b85a789c 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)