-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path01 Grading Students.swift
47 lines (38 loc) · 1.16 KB
/
01 Grading Students.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// 01 Grading Students.swift
// HackerRank-Solutions/Problem Solving/Algorithms/02 Implementation
//
// Created by Aleksandar Dinic on 09/06/2020.
// Copyright © 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://www.hackerrank.com/challenges/grading/problem
struct Solution {
/// Automates the rounding grade process.
///
/// - Parameter grades: Student's grades.
/// - Returns: The rounded grade for each grade.
///
/// - Complexity:
/// - time: O(n), where n is the number of grades.
/// - space: O(n), where n is the number of grades.
func gradingStudents(_ grades: [Int]) -> [Int] {
var ans = [Int]()
for grade in grades {
if grade < 38 || grade % 5 <= 2 {
ans.append(grade)
} else {
ans.append(grade + 5 - (grade % 5))
}
}
return ans
}
}
let gradesCount = Int(readLine()!)!
var grades = [Int]()
for _ in 0..<gradesCount {
grades.append(Int(readLine()!)!)
}
let solution = Solution()
let ans = solution.gradingStudents(grades)
print(ans.map { String($0) }.joined(separator: "\n"))