diff --git a/src/ex2.py b/src/ex2.py index 2ce418ef..a6780b3e 100644 --- a/src/ex2.py +++ b/src/ex2.py @@ -1,51 +1,31 @@ -# -# 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; -# } +""" + + +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))) 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) == "--"