Algorithm
Problem Name: Algorithms -
In this HackerRank Functions in Algorithms - Java programming problem solution,
HackerLand University has the following grading policy:
- Every student receives a grade in the inclusive range from 0 to 100.
- Any grade less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student’s grade according to these rules:
- If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.
- If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.
Examples
- grade = 84 round to 85 (85 – 84 is less than 3)
- grade = 29 do not round (result is less than 40)
- grade do not round (60 – 57 is 3 or higher)
Given the initial value of for each of Sam’s students, write code to automate the rounding process.
Function Description
Complete the function gradingStudents in the editor below.
gradingStudents has the following parameter(s):
- int grades[n]: the grades before rounding
Returns
- int[n]: the grades after rounding as appropriate
Input Format
The first line contains a single integer,n, the number of students.
Each line i of the n subsequent lines contains a single integer, grade[i].
Constraints
- 1<=n<=60
- 0<=grades[i]<=100
Sample Input 0
4 73 67 38 33
Sample Output 0
75 67 40 33
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
scanf("%d",&n);
for(int a0 = 0; a0 < n; a0++){
int grade;
float go;
scanf("%d",&grade);
go = (float)grade/5;
go = ceil(go);
go = go * 5;
//printf("%f\n", go);
if(grade >= 38){
if(go - grade < 3){
grade = (int)go;
}
}
printf("%d\n",grade>;
}
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
{
cin>>a[i];
}
for(int i = 0; i < n; i++)
{
if(a[i] >= 38){
if((a[i]%5) == 3)
{
a[i] = a[i]+2;
}
if((a[i]%5) == 4)
{
a[i] = a[i] + 1;}
}
}
for(int i = 0; i < n; i++>
{
cout << a[i] << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int[] solve(int[] grades){
for (int i = 0; i < grades.length; i++) {
if (grades[i] >= 38) {
int nextMultipleOfFive = 5 - (grades[i] % 5) + grades[i];
if (nextMultipleOfFive - grades[i] < 3) {
grades[i] = nextMultipleOfFive;
}
}
}
return grades;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] grades = new int[n];
for(int grades_i = 0; grades_i < n; grades_i++){
grades[grades_i] = in.nextInt();
}
int[] result = solve(grades);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + (i != result.length - 1 ? "\n" : ""));
}
System.out.println("");
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Javascript Programming
Code -
Javascript Programming
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'gradingStudents' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY grades as parameter.
*/
function gradingStudents(grades) {
// the problem is n neareste whole number.
return grades.map((value)=>{
//const roundNumber=Math.abs((value%5)-5)+value;
const roundNumber=Math.ceil(value/5)*5;
return (roundNumber-value) < 3 && roundNumber>=40?roundNumber:value
})
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const gradesCount = parseInt(readLine().trim(), 10);
let grades = [];
for (let i = 0; i < gradesCount; i++) {
const gradesItem = parseInt(readLine().trim(), 10);
grades.push(gradesItem);
}
const result = gradingStudents(grades);
ws.write(result.join('\n') + '\n');
ws.end();
}
Copy The Code &
Try With Live Editor
#5 Code Example with C# Programming
Code -
C# Programming
using System;
class Solution
{
static int[] solve(int[] grades)
{
for (int i = 0; i < grades.Length; i++)
{
var item = grades[i];
if (item >= 38)
{
var diff = 5 - (item % 5);
if (diff < 3)
grades[i] = item + diff;
}
}
return grades;
}
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] grades = new int[n];
for (int grades_i = 0; grades_i < n; grades_i++)
grades[grades_i] = Convert.ToInt32(Console.ReadLine());
int[] result = solve(grades);
Console.WriteLine(String.Join("\n", result));
}
}
Copy The Code &
Try With Live Editor
#6 Code Example with Python Programming
Code -
Python Programming
import sys
import math
def solve(grades):
# Complete this function
toReturn = ""
for grade in grades:
newGrade = 0
c = grade % 5
if(grade >= 38 and 5-c < 3 and c != 0>:
newGrade = grade + 5- c
if (newGrade > 100):
newGrade = 100
print(str(newGrade))
else:
newGrade = grade
print(str(newGrade))
n = int(input().strip())
grades = []
grades_i = 0
for grades_i in range(n):
grades_t = int(input().strip())
grades.append(grades_t)
result = solve(grades)
Copy The Code &
Try With Live Editor
#7 Code Example with PHP Programming
Code -
PHP Programming
= 38) {
$m = $grades[$i]% 5;
if ($m == 3) {
$grades[$i] += 2;
}
if ($m == 4) {
$grades[$i] += 1;
}
}
}
return $grades;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$__fp = fopen("php://stdin", "r");
fscanf($__fp, "%d\n", $n);
$grades = array();
for ($grades_itr = 0; $grades_itr < $n; $grades_itr++) {
fscanf($__fp, "%d\n", $grades_item);
$grades[] = $grades_item;
}
$result = gradingStudents($grades);
fwrite($fptr, implode("\n", $result) . "\n");
fclose($__fp);
fclose($fptr>;
Copy The Code &
Try With Live Editor