Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created August 19, 2022 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagozs/c6f4f25f92a06aa82f862afd248b7175 to your computer and use it in GitHub Desktop.
Save thiagozs/c6f4f25f92a06aa82f862afd248b7175 to your computer and use it in GitHub Desktop.
HackerRank gradingStudents
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"math"
)
/*
* Complete the 'gradingStudents' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY grades as parameter.
*/
func gradingStudents(grades []int32) []int32 {
// Write your code here
var finalGrades []int32
for i := 0; i < len(grades); i++ {
quotient := float64(grades[i]) / float64(5)
rounded := int32(math.Ceil(quotient) * 5)
difference := rounded - grades[i]
if grades[i] > 34 {
if difference < 3 {
finalGrades = append(finalGrades, rounded)
} else {
finalGrades = append(finalGrades, grades[i])
}
}
if grades[i] < 35 {
finalGrades = append(finalGrades, grades[i])
}
}
return finalGrades
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024)
gradesCount, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
var grades []int32
for i := 0; i < int(gradesCount); i++ {
gradesItemTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
gradesItem := int32(gradesItemTemp)
grades = append(grades, gradesItem)
}
result := gradingStudents(grades)
for i, resultItem := range result {
fmt.Fprintf(writer, "%d", resultItem)
if i != len(result) - 1 {
fmt.Fprintf(writer, "\n")
}
}
fmt.Fprintf(writer, "\n")
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment