From c403c479d714934e204448ea16e146947cb9de4b Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Thu, 30 Oct 2025 17:50:17 +0100 Subject: [PATCH 1/6] refactor: replacing if statement with switch --- exercises/ex1.cpp | 58 +++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/exercises/ex1.cpp b/exercises/ex1.cpp index 22069589..a20efb90 100644 --- a/exercises/ex1.cpp +++ b/exercises/ex1.cpp @@ -11,38 +11,32 @@ int main() 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; + switch(week){ + case 1: + cout << "Monday" << endl; + break; + case 2: + cout << "Tuesday" << endl; + break; + case 3: + cout << "Wednesday" << endl; + break; + case 4: + cout << "Thursday" << endl; + break; + case 5: + cout << "Friday" << endl; + break; + case 6: + cout << "Saturday" << endl; + break; + case 7: + cout << "Sunday" << endl; + break; + default: + cout << "Invalid input! Please enter week number between 1-7." << endl; + break; } return 0; -} \ No newline at end of file +} From 648684e00cfe1392ee2bd02225e95e093898c138 Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Mon, 3 Nov 2025 11:05:57 +0100 Subject: [PATCH 2/6] Fix:indentation --- exercises/ex1.cpp | 53 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/exercises/ex1.cpp b/exercises/ex1.cpp index a20efb90..b43228e9 100644 --- a/exercises/ex1.cpp +++ b/exercises/ex1.cpp @@ -3,39 +3,38 @@ #include using namespace std; -int main() -{ +int main() { int week; /* Input week number from user */ cout << "Enter week number(1-7): " << endl; cin >> week; - switch(week){ - case 1: - cout << "Monday" << endl; - break; - case 2: - cout << "Tuesday" << endl; - break; - case 3: - cout << "Wednesday" << endl; - break; - case 4: - cout << "Thursday" << endl; - break; - case 5: - cout << "Friday" << endl; - break; - case 6: - cout << "Saturday" << endl; - break; - case 7: - cout << "Sunday" << endl; - break; - default: - cout << "Invalid input! Please enter week number between 1-7." << endl; - break; + switch (week) { + case 1: + cout << "Monday" << endl; + break; + case 2: + cout << "Tuesday" << endl; + break; + case 3: + cout << "Wednesday" << endl; + break; + case 4: + cout << "Thursday" << endl; + break; + case 5: + cout << "Friday" << endl; + break; + case 6: + cout << "Saturday" << endl; + break; + case 7: + cout << "Sunday" << endl; + break; + default: + cout << "Invalid input! Please enter week number between 1-7." << endl; + break; } return 0; From eac06289091994fc2878e0da8627faefbc621c66 Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Mon, 3 Nov 2025 11:33:02 +0100 Subject: [PATCH 3/6] Revert "Fix:indentation" This reverts commit 648684e00cfe1392ee2bd02225e95e093898c138. --- exercises/ex1.cpp | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/exercises/ex1.cpp b/exercises/ex1.cpp index b43228e9..a20efb90 100644 --- a/exercises/ex1.cpp +++ b/exercises/ex1.cpp @@ -3,38 +3,39 @@ #include using namespace std; -int main() { +int main() +{ int week; /* Input week number from user */ cout << "Enter week number(1-7): " << endl; cin >> week; - switch (week) { - case 1: - cout << "Monday" << endl; - break; - case 2: - cout << "Tuesday" << endl; - break; - case 3: - cout << "Wednesday" << endl; - break; - case 4: - cout << "Thursday" << endl; - break; - case 5: - cout << "Friday" << endl; - break; - case 6: - cout << "Saturday" << endl; - break; - case 7: - cout << "Sunday" << endl; - break; - default: - cout << "Invalid input! Please enter week number between 1-7." << endl; - break; + switch(week){ + case 1: + cout << "Monday" << endl; + break; + case 2: + cout << "Tuesday" << endl; + break; + case 3: + cout << "Wednesday" << endl; + break; + case 4: + cout << "Thursday" << endl; + break; + case 5: + cout << "Friday" << endl; + break; + case 6: + cout << "Saturday" << endl; + break; + case 7: + cout << "Sunday" << endl; + break; + default: + cout << "Invalid input! Please enter week number between 1-7." << endl; + break; } return 0; From 2f814023a9b07c5c8f057261b3714df6f5907a7e Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Mon, 3 Nov 2025 11:35:10 +0100 Subject: [PATCH 4/6] Revert "refactor: replacing if statement with switch" This reverts commit c403c479d714934e204448ea16e146947cb9de4b. --- exercises/ex1.cpp | 58 ++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/exercises/ex1.cpp b/exercises/ex1.cpp index a20efb90..22069589 100644 --- a/exercises/ex1.cpp +++ b/exercises/ex1.cpp @@ -11,32 +11,38 @@ int main() cout << "Enter week number(1-7): " << endl; cin >> week; - switch(week){ - case 1: - cout << "Monday" << endl; - break; - case 2: - cout << "Tuesday" << endl; - break; - case 3: - cout << "Wednesday" << endl; - break; - case 4: - cout << "Thursday" << endl; - break; - case 5: - cout << "Friday" << endl; - break; - case 6: - cout << "Saturday" << endl; - break; - case 7: - cout << "Sunday" << endl; - break; - default: - cout << "Invalid input! Please enter week number between 1-7." << endl; - break; + 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; -} +} \ No newline at end of file From 35cadb6876761519fba8435146620861974f89cb Mon Sep 17 00:00:00 2001 From: Giuseppe Tornello Date: Mon, 3 Nov 2025 12:50:15 +0100 Subject: [PATCH 5/6] refactor: replaced if statement with map --- exercises/ex3.cpp | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 025d77c8..3c3e3844 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -1,42 +1,34 @@ /* Could you still use a switch case here? May you can use a map. */ #include +#include using namespace std; +unordered_map initialize_peopleMap(){ + unordered_map peopleMap = { + {"BarackObama","44th president of the United States"}, + {"SandroPertini","Former President of the Italian Republic"}, + {"NelsonMandela","Former President of South Africa"}, + {"MahatmaGandhi","Bapu"}, + {"DonaldKnuth","Creator of LaTeX"}, + {"DennisRitchie","Creator of C"} + }; + return peopleMap; +} + int main() { string textInput; + unordered_map peopleMap = initialize_peopleMap(); cout << "Enter a famous name+surname, ex. BarackObama " << endl; cin >> textInput; - if (textInput == "BarackObama") - { - cout << "44th president of the United States" << endl; - } - else if (textInput == "SandroPertini") - { - cout << "Former President of the Italian Republic" << endl; - } - else if (textInput == "NelsonMandela") - { - cout << "Former President of South Africa" << endl; - } - else if (textInput == "MahatmaGandhi") - { - cout << "Bapu" << endl; - } - else if (textInput == "DonaldKnuth") - { - cout << "Creator of LaTeX" << endl; - } - else if (textInput == "DennisRitchie") - { - cout << "Creator of C" << endl; - } - else - { + if(peopleMap.count(textInput) > 0){ + cout< Date: Mon, 3 Nov 2025 15:24:28 +0100 Subject: [PATCH 6/6] chore: added exit defines and improved readability --- exercises/ex3.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 3c3e3844..aca95dd0 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -4,6 +4,10 @@ #include using namespace std; +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + + unordered_map initialize_peopleMap(){ unordered_map peopleMap = { {"BarackObama","44th president of the United States"}, @@ -24,12 +28,12 @@ int main() cout << "Enter a famous name+surname, ex. BarackObama " << endl; cin >> textInput; - if(peopleMap.count(textInput) > 0){ - cout< 0){ //checks if there is a name like textInput in the peopleMap + cout << peopleMap[textInput] << endl; } else { cout << "Invalid input! Please enter a good name!" << endl; - return 1; + return EXIT_FAILURE; } - return 0; + return EXIT_SUCCESS; } \ No newline at end of file