├── .gitignore ├── exercises ├── random_number.cpp ├── binary_converter.cpp ├── sum.cpp ├── calculator.cpp ├── verify.cpp ├── risk-risiko.cpp ├── ex1.cpp ├── ex2.cpp ├── ex4.cpp └── ex3.cpp ├── compile.sh ├── .github └── workflows │ ├── ci.yml │ ├── codestyle.yml │ └── cpp-check.yml ├── codestyle.sh └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | report.txt 3 | 4 | -------------------------------------------------------------------------------- /exercises/random_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that generates a random number. 3 | 4 | Output: 5 | The random number is: 4 6 | */ 7 | -------------------------------------------------------------------------------- /compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for FILE in exercises/*.cpp; do 3 | printf "Compiling: $FILE ->\n" 4 | g++ $FILE -std=c++11 -c -Wall -Werror -Wextra 5 | done 6 | -------------------------------------------------------------------------------- /exercises/binary_converter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that given a number as input convert it in binary. 3 | 4 | Output: 5 | Insert first number: 8 6 | The binary number is: 1000 7 | */ 8 | -------------------------------------------------------------------------------- /exercises/sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes as input two numbers and print the sum. 3 | 4 | Output: 5 | Insert the first number: 1 6 | Insert the second number: 2 7 | Sum: 3 8 | */ 9 | -------------------------------------------------------------------------------- /exercises/calculator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that given two numbers as input make the main operations. 3 | 4 | Output: 5 | Insert first number: 4 6 | Insert second number: 2 7 | 8 | SUM: 6 9 | Difference: 2 10 | Multiplication: 8 11 | Division: 2 12 | */ 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | branches: [main] 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: install g++ 13 | run: sudo apt install -y g++ 14 | - name: check build 15 | run: source ./compile.sh 16 | -------------------------------------------------------------------------------- /.github/workflows/codestyle.yml: -------------------------------------------------------------------------------- 1 | name: check-codestyle 2 | on: 3 | pull_request: 4 | 5 | jobs: 6 | check-codestyle: 7 | strategy: 8 | fail-fast: false 9 | runs-on: ubuntu-latest 10 | name: check codestyle 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: Check core codestyle 14 | run: source ./codestyle.sh 15 | 16 | -------------------------------------------------------------------------------- /exercises/verify.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a software that verifies if a number is present in a pre-defined array. 3 | 4 | Output example: 5 | Insert number 3 6 | The number 3 is [not] present in the array. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | // placeholder 15 | int N[10] = {3, 4, 5, 1, 2, 3, 4, 9, 13, 0}; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /codestyle.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | echo "Codestyle check script:" 5 | echo 6 | 7 | declare -A singleLineRegexChecks=( 8 | ["[[:blank:]]$"]="Remove whitespace at the end of the lines above" 9 | ["\t"]="Replace tabs with 4 spaces in the lines above" 10 | ) 11 | 12 | for check in ${!singleLineRegexChecks[@]}; do 13 | echo " Checking RegEx: '${check}'" 14 | 15 | if grep -P -r -I -n ${check} exercises/ | grep .cpp; then 16 | echo 17 | echo "${singleLineRegexChecks[$check]}" 18 | exit 1 19 | fi 20 | done 21 | 22 | echo 23 | echo "Everything looks good" 24 | -------------------------------------------------------------------------------- /.github/workflows/cpp-check.yml: -------------------------------------------------------------------------------- 1 | name: cpp-check 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - "main" 7 | 8 | jobs: 9 | cpp-check: 10 | strategy: 11 | fail-fast: false 12 | runs-on: [ubuntu-22.04] 13 | name: cpp check 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Install dependencies 17 | run: | 18 | sudo apt update -y 19 | sudo apt install -y cppcheck 20 | - name: cpp check 21 | run: | 22 | cppcheck --force --inline-suppr --output-file=report.txt exercises/ 23 | if [ -s report.txt ]; then # if file is not empty 24 | cat report.txt 25 | exit 1 # let github action fails 26 | fi 27 | -------------------------------------------------------------------------------- /exercises/risk-risiko.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that simulates a risk/risiko fight using 6 dices. 3 | 4 | How does it work? 5 | When a player attacks another player he uses 3 dices, the red is always the attacker and the blue is the defender. 6 | 7 | You have to compare the dice with the highest number to simulate the fight. 8 | N = first highest number 9 | M = second highest number 10 | O = third highest number 11 | 12 | If the numbers are equal, the defensor (blue) wins. 13 | 14 | Output: 15 | Red dices: 16 | 6 (N) 17 | 3 (M) 18 | 2 (O) 19 | 20 | Blue dices: 21 | 5 (N) 22 | 3 (M) 23 | 1 (O) 24 | 25 | R B 26 | N 6 vs 5 => red win 27 | M 3 vs 3 => blue win 28 | O 2 vs 1 => red win 29 | */ 30 | -------------------------------------------------------------------------------- /exercises/ex1.cpp: -------------------------------------------------------------------------------- 1 | /* Improve this program using a switch-case. */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int week; 9 | 10 | /* Input week number from user */ 11 | cout << "Enter week number(1-7): " << endl; 12 | cin >> week; 13 | 14 | if (week == 1) 15 | { 16 | cout << "Monday" << endl; 17 | } 18 | else if (week == 2) 19 | { 20 | cout << "Tuesday" << endl; 21 | } 22 | else if (week == 3) 23 | { 24 | cout << "Wednesday" << endl; 25 | } 26 | else if (week == 4) 27 | { 28 | cout << "Thursday" << endl; 29 | } 30 | else if (week == 5) 31 | { 32 | cout << "Friday" << endl; 33 | } 34 | else if (week == 6) 35 | { 36 | cout << "Saturday" << endl; 37 | } 38 | else if (week == 7) 39 | { 40 | cout << "Sunday" << endl; 41 | } 42 | else 43 | { 44 | cout << "Invalid input! Please enter week number between 1-7." << endl; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /exercises/ex2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Improve this program replacing if/else if with an array. 3 | 4 | Hint: arr[3] = "Thursday"; 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int week; 13 | 14 | cout << "Enter week number(1-7): " << endl; 15 | cin >> week; 16 | 17 | if (week == 1) 18 | { 19 | cout << "Monday" << endl; 20 | } 21 | else if (week == 2) 22 | { 23 | cout << "Tuesday" << endl; 24 | } 25 | else if (week == 3) 26 | { 27 | cout << "Wednesday" << endl; 28 | } 29 | else if (week == 4) 30 | { 31 | cout << "Thursday" << endl; 32 | } 33 | else if (week == 5) 34 | { 35 | cout << "Friday" << endl; 36 | } 37 | else if (week == 6) 38 | { 39 | cout << "Saturday" << endl; 40 | } 41 | else if (week == 7) 42 | { 43 | cout << "Sunday" << endl; 44 | } 45 | else 46 | { 47 | cout << "Invalid input! Please enter week number between 1-7." << endl; 48 | } 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /exercises/ex4.cpp: -------------------------------------------------------------------------------- 1 | /* Surprise me. */ 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int month; 8 | 9 | /* Input month number from user */ 10 | printf("Enter month number(1-12): "); 11 | scanf("%d", &month); 12 | 13 | switch (month) 14 | { 15 | case 1: 16 | printf("31 days"); 17 | break; 18 | case 2: 19 | printf("28/29 days"); 20 | break; 21 | case 3: 22 | printf("31 days"); 23 | break; 24 | case 4: 25 | printf("30 days"); 26 | break; 27 | case 5: 28 | printf("31 days"); 29 | break; 30 | case 6: 31 | printf("30 days"); 32 | break; 33 | case 7: 34 | printf("31 days"); 35 | break; 36 | case 8: 37 | printf("31 days"); 38 | break; 39 | case 9: 40 | printf("30 days"); 41 | break; 42 | case 10: 43 | printf("31 days"); 44 | break; 45 | case 11: 46 | printf("30 days"); 47 | break; 48 | case 12: 49 | printf("31 days"); 50 | break; 51 | default: 52 | printf("Invalid input! Please enter month number between 1-12"); 53 | } 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /exercises/ex3.cpp: -------------------------------------------------------------------------------- 1 | /* Could you still use a switch case here? May you can use a map. */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | string textInput; 9 | 10 | cout << "Enter a famous name+surname, ex. BarackObama " << endl; 11 | cin >> textInput; 12 | 13 | if (textInput == "BarackObama") 14 | { 15 | cout << "44th president of the United States" << endl; 16 | } 17 | else if (textInput == "SandroPertini") 18 | { 19 | cout << "Former President of the Italian Republic" << endl; 20 | } 21 | else if (textInput == "NelsonMandela") 22 | { 23 | cout << "Former President of South Africa" << endl; 24 | } 25 | else if (textInput == "MahatmaGandhi") 26 | { 27 | cout << "Bapu" << endl; 28 | } 29 | else if (textInput == "DonaldKnuth") 30 | { 31 | cout << "Creator of LaTeX" << endl; 32 | } 33 | else if (textInput == "DennisRitchie") 34 | { 35 | cout << "Creator of C" << endl; 36 | } 37 | else 38 | { 39 | cout << "Invalid input! Please enter a good name!" << endl; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-course-exercises 2 | 3 | ### 1️⃣ How to start 4 | 5 | Fork this repository and clone your fork into your machine using: 6 | ```bash 7 | git clone git@github.com:USERNAME/git-course-exercises.git 8 | ``` 9 | 10 | enter to your project directory using: 11 | ```bash 12 | cd git-course-exercises 13 | ``` 14 | 15 | --- 16 | 17 | ### 2️⃣ How to make a branch 18 | 19 | To create a new `branch` from the main repository and work on a specific exercise (ex. `file.cpp`) you can use the following commands: 20 | ```bash 21 | git checkout main 22 | 23 | git checkout -b new-branch-name 24 | ``` 25 | 26 | --- 27 | 28 | ### 3️⃣ How to make a commit 29 | 30 | From this step you can edit your `file.cpp` and make some commits using: 31 | ```bash 32 | git add exercises/file.cpp 33 | 34 | git commit -m 'feat: description' 35 | ``` 36 | Use the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) when you write a commit description. 37 | 38 | --- 39 | 40 | ### 4️⃣ How to make a PR 41 | 42 | Push the commits into GitHub using: 43 | ```bash 44 | git push -u origin new-branch-name 45 | ``` 46 | 47 | Afterward, it is possible to create a Pull Request by going to your fork `https://github.com/USERNAME/git-course-exercises.git` clicking on "create Pull Request" or going to the right section "Pull Request" -> "New Pull Request". 48 | 49 | If you want to work on another `file.cpp`, go to step 2️⃣. 50 | 51 | --- 52 | 53 | Use git wisely. 54 | 55 | ### Clean code 56 | 57 | Check [this article](https://testdriven.io/blog/clean-code-python/) to understand how to write "clean code" in python. 58 | 59 | ### Contributing 60 | 61 | Feel free to improve this README with any other detail, if you do it, I'll be grateful. 62 | --------------------------------------------------------------------------------