반응형
Grade book
Complete the function so that it finds the average of the three scores passed to it and returns the letter value associated with that grade.
Numerical Score Letter Grade
90 <= score <= 100 'A'
80 <= score < 90 'B'
70 <= score < 80 'C'
60 <= score < 70 'D'
0 <= score < 60 'F'
Tested values are all between 0 and 100. Theres is no need to check for negative values or values greater than 100.
Solution:
String getGrade(int a, int b, int c) {
List arr = [a, b, c];
int sum = arr.reduce((a, b) => a + b);
int len = arr.length;
double avg = sum/len;
if (avg < 60) {
return "F";
} else if (avg < 70) {
return "D";
} else if (avg < 80) {
return "C";
} else if (avg < 90) {
return "B";
} else {
return "A";
}
}
String getGrade(int a, int b, int c) {
int mean = (a + b + c) ~/ 3;
if (mean >= 90) return 'A';
else if (mean >= 80) return 'B';
else if (mean >= 70) return 'C';
else if (mean >= 60) return 'D';
else return 'F';
}
String getGrade(int a, int b, int c) {
switch ((a + b + c) ~/ 30) {
case 10: return 'A';
case 9: return 'A';
case 8: return 'B';
case 7: return 'C';
case 6: return 'D';
default: return 'F';
}
}
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1123. L1: Set Alarm (0) | 2022.11.24 |
---|---|
1122. Grasshopper - Messi goals function (0) | 2022.11.22 |
1120. Determine offspring sex based on genes XX and XY chromosomes (0) | 2022.11.20 |
1119. Return Negative (0) | 2022.11.19 |
1118. Function 1 - hello world (0) | 2022.11.18 |