Browse Source

feat(grades): add subject, test and solution for the exercise

DEV-4397-piscine-ai-missing-file-for-ex-7-of-nlp
miguel 2 years ago committed by MSilva95
parent
commit
a1b76b0f0b
  1. 104
      sh/tests/grades_test.sh
  2. 29
      sh/tests/solutions/grades.sh
  3. 108
      subjects/devops/grades/README.md

104
sh/tests/grades_test.sh

@ -0,0 +1,104 @@
#!/usr/bin/env bash
IFS='
'
script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
challenge() {
declare -a equals=("Student1 90" "Student2 70" "Student3 50")
declare -a differents=("Student1 92" "Student2 75" "Student3 55" "Student4 25")
for e in "${equals[@]}"; do
name=$(echo "$e" | awk '{print $1}')
grade=$(echo "$e" | awk '{print $2}')
submitted=$(echo "$name $grade" | bash -c ""$script_dirS"/student/grades.sh 1")
expected=$(echo "$name $grade" | bash -c ""$script_dirS"/solutions/grades.sh 1")
diff <(echo "$submitted") <(echo "$expected")
done
for d in "${differents[@]}"; do
name=$(echo "$d" | awk '{print $1}')
grade=$(echo "$d" | awk '{print $2}')
submitted=$(echo "$name $grade" | bash -c ""$script_dirS"/student/grades.sh 1")
expected=$(echo "$name $grade" | bash -c ""$script_dirS"/solutions/grades.sh 1")
diff <(echo "$submitted") <(echo "$expected")
done
# Testing multiple students
submitted=$(
bash -c ""$script_dirS"/student/grades.sh 3" <<EOF
Bob
90
Alice
75
Eve
55
EOF
)
expected=$(
bash -c ""$script_dirS"/solutions/grades.sh 3" <<EOF
Bob
90
Alice
75
Eve
55
EOF
)
diff <(echo "$submitted") <(echo "$expected")
submitted=$(
bash -c ""$script_dirS"/student/grades.sh 5" <<EOF
Bob
90
Alice
75
Eve
55
john
49
Eric
65
EOF
)
expected=$(
bash -c ""$script_dirS"/solutions/grades.sh 5" <<EOF
Bob
90
Alice
75
Eve
55
john
49
Eric
65
EOF
)
diff <(echo "$submitted") <(echo "$expected")
# Checking if it fails with invallid grades
submitted=$(
bash -c ""$script_dirS"/student/grades.sh 3" <<EOF
Bob
90
Alice
150
Eve
55
EOF
)
expected=$(
bash -c ""$script_dirS"/solutions/grades.sh 3" <<EOF
Bob
90
Alice
150
Eve
55
EOF
)
diff <(echo "$submitted") <(echo "$expected")
}
challenge

29
sh/tests/solutions/grades.sh

@ -0,0 +1,29 @@
#!/usr/bin/env bash
num_students=$1
declare -a students
for ((i = 0; i < num_students; i++)); do
read -p "Student Name #$((i + 1)): " name
read -p "Student Grade #$((i + 1)): " grade
students+=("$name $grade")
if [ $(expr "$grade" \> 100) -eq 1 ] || ! [[ "$grade" =~ ^[0-9]+$ ]]; then
echo "Error: The grade '$grade' is not a valid input. Only numerical grades between 0 and 100 are accepted."
exit 1
fi
done
for student in "${students[@]}"; do
name=$(echo $student | awk '{print $1}')
grade=$(echo $student | awk '{print $2}')
if [ "$grade" -ge 90 ]; then
echo "$name: You did an excellent job!"
elif [ "$grade" -ge 70 ]; then
echo "$name: You did a good job!"
elif [ "$grade" -ge 50 ]; then
echo "$name: You need a bit more effort!"
else
echo "$name: You had a poor performance!"
fi
done

108
subjects/devops/grades/README.md

@ -0,0 +1,108 @@
## grades
### Instructions
Create a script `grades.sh` which will ask the user for a specific number of students in order to evaluate them. After the number is set, the script will ask you to introduce the "names" and "grades" of the persons you wish to evaluate.
The grades will have a range between 0 and 100 and will be numeric values only.
According to each student grade you will have to return the following:
- If the student grade is anything lower than 50 you will return the string "<name>: You had a poor performance!":
```console
$ ./grades.sh 1
Student Name #1: Sara
Student Grade #1: 34
Sara: You had a poor performance!
```
- If the student grade is anything equal or greater than 50 you will return the string "<name>: You need a bit more effort!":
```console
$ ./grades.sh 1
Student Name #1: Sara
Student Grade #1: 51
Sara: You need a bit more effort!
```
- If the student grade is anything equal or greater than 70 you will return the string "<name>: You did a good job!":
```console
$ ./grades.sh 1
Student Name #1: Sara
Student Grade #1: 75
Sara: You did a good job!
```
- If the student grade is anything equal or greater than 90 you will return the string "<name>: You did an excellent job!":
```console
$ ./grades.sh 1
Student Name #1: Sara
Student Grade #1: 90
Sara: You did an excellent job!
```
### Error handling
All errors will print a specific message on **stderr** (ending with a newline) and returns a specific non-zero value:
- Wrong number of arguments: `"Error: expect 2 arguments"`, exit with `1`.
- If the student grade is not a number or is greater than 100: `Error: The grade "grade" is not a valid input. Only numerical grades between 0 and 100 are accepted.`, exit with `1`.
```console
$ ./grades.sh 3
./grades.sh 2
Student Name #1: Sara
Student Grade #1: 101
Error: The grade '101' is not a valid input. Only numerical grades between 0 and 100 are accepted.
$
```
### Usage
```console
$ ./grades.sh 4
Student Name #1: Sara # introduced by the user
Student Grade #1: 34 # introduced by the user
Student Name #2: Norman
Student Grade #2: 56
Student Name #3: James
Student Grade #3: 78
Student Name #4: Albert
Student Grade #4: 90
Sara: You had a poor performance!
Norman: You need a bit more effort!
James: You did a good job!
Albert: You did an excellent job!
```
### Hints
In bash, you can use the `read` command to read input from the user and store it in a variable like so:
`read var_name`
You can also use the "-p" option to specify a prompt that will be displayed to the user before reading the input.
Use loops to go through each student. For example the for loop is very helpful.
```console
for element in sequence
do
# commands to be executed
done
```
You can use an array to store multiple pieces of data. To create an array, you can use the `declare` command with the "-a" option:
`declare -a name_array`
To add elements to the array, you can use the += operator:
`name_array+=("Adding this to the array")`
> You have to use Man or Google to know more about commands flags, in order to solve this exercise!
> Google and Man will be your friends!
Loading…
Cancel
Save