From 4d5f0e68e366b54cc06fca9a14e21d09a0162c99 Mon Sep 17 00:00:00 2001 From: LeleValore Date: Thu, 27 Nov 2025 18:22:09 +0100 Subject: [PATCH 1/3] feat: implemented ex2.py --- src/ex2.py | 70 ++++++++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 50 deletions(-) diff --git a/src/ex2.py b/src/ex2.py index 2ce418ef..a7f0e86f 100644 --- a/src/ex2.py +++ b/src/ex2.py @@ -1,51 +1,21 @@ -# -# Implement the following code in Python replacing if/else if with an array. -# -# Hint: arr[3] = "Thursday"; -# +"""Scrivere in python un esercizio che prenda un numero da 1 a 7 e restituisca il giorno della settimana corrispondente. +Utilizzare un array per memorizzare i nomi dei giorni della settimana invece di utilizzare una serie di istruzioni if/else if. +""" -# #include -# using namespace std; -# -# int main() -# { -# int week; -# -# cout << "Enter week number(1-7): " << endl; -# cin >> week; -# -# if (week == 1) -# { -# cout << "Monday" << endl; -# } -# else if (week == 2) -# { -# cout << "Tuesday" << endl; -# } -# else if (week == 3) -# { -# cout << "Wednesday" << endl; -# } -# else if (week == 4) -# { -# cout << "Thursday" << endl; -# } -# else if (week == 5) -# { -# cout << "Friday" << endl; -# } -# else if (week == 6) -# { -# cout << "Saturday" << endl; -# } -# else if (week == 7) -# { -# cout << "Sunday" << endl; -# } -# else -# { -# cout << "Invalid input! Please enter week number between 1-7." << endl; -# } -# -# return 0; -# } +# Giorni della settimana memorizzati in un array +giorni_settimana = [ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", +] +# Input dell'utente +numero_settimana = int(input("Enter week number (1-7): ")) +# Verifica se l'input è valido e stampa il giorno corrispondente +if 1 <= numero_settimana <= 7: + print(giorni_settimana[numero_settimana - 1]) # Sottrai 1 per l'indice dell'array +else: + print("Invalid input! Please enter week number between 1-7.") From 3849a5483ea9f56daee59958f8172d370a4e854b Mon Sep 17 00:00:00 2001 From: LeleValore Date: Thu, 27 Nov 2025 18:35:52 +0100 Subject: [PATCH 2/3] Refactor: add function giorno_della_settimana --- src/ex2.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/ex2.py b/src/ex2.py index a7f0e86f..a6780b3e 100644 --- a/src/ex2.py +++ b/src/ex2.py @@ -1,21 +1,31 @@ """Scrivere in python un esercizio che prenda un numero da 1 a 7 e restituisca il giorno della settimana corrispondente. Utilizzare un array per memorizzare i nomi dei giorni della settimana invece di utilizzare una serie di istruzioni if/else if. + """ -# Giorni della settimana memorizzati in un array -giorni_settimana = [ - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday", - "Sunday", -] -# Input dell'utente -numero_settimana = int(input("Enter week number (1-7): ")) -# Verifica se l'input è valido e stampa il giorno corrispondente -if 1 <= numero_settimana <= 7: - print(giorni_settimana[numero_settimana - 1]) # Sottrai 1 per l'indice dell'array -else: - print("Invalid input! Please enter week number between 1-7.") + +def giorno_della_settimana(n: int) -> str: + + match n: + case 1: + return "Lunedì" + case 2: + return "Martedì" + case 3: + return "Mercoledì" + case 4: + return "Giovedì" + case 5: + return "Venerdì" + case 6: + return "Sabato" + case 7: + return "Domenica" + case _: + return "--" + + +if __name__ == "__main__": + + s = input("Scegli un giorno della settimana: ") + print("Il girono della settimana scelto: è ", giorno_della_settimana(int(s))) From 8b8be511121b7640a2308521ace856512a45771b Mon Sep 17 00:00:00 2001 From: LeleValore Date: Thu, 27 Nov 2025 18:43:50 +0100 Subject: [PATCH 3/3] Feat: Ex2_Test_function --- tests/test_ex2.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/test_ex2.py diff --git a/tests/test_ex2.py b/tests/test_ex2.py new file mode 100644 index 00000000..3f61ba41 --- /dev/null +++ b/tests/test_ex2.py @@ -0,0 +1,13 @@ +from src.ex2 import giorno_della_settimana + + +def test_giorno_della_settimana(): + assert giorno_della_settimana(1) == "Lunedì" + assert giorno_della_settimana(2) == "Martedì" + assert giorno_della_settimana(3) == "Mercoledì" + assert giorno_della_settimana(4) == "Giovedì" + assert giorno_della_settimana(5) == "Venerdì" + assert giorno_della_settimana(6) == "Sabato" + assert giorno_della_settimana(7) == "Domenica" + assert giorno_della_settimana(0) == "--" + assert giorno_della_settimana(8) == "--"