Showing posts with label grading policy question hackerrank. Show all posts
Showing posts with label grading policy question hackerrank. Show all posts

Tuesday, December 21, 2021

JAVA End Of File

 The challenge here is to read  lines of input until you reach EOF, then number and print all  lines of content.

Hint: Java's Scanner.hasNext() method is helpful for this problem.

Input Format

Read some unknown  lines of input from stdin(System.in) until you reach EOF; each line of input contains a non-empty String.

Output Format

For each line, print the line number, followed by a single space, and then the line content received as input.

Sample Input

Hello world
I am a file
Read me until end-of-file.

Sample Output

1 Hello world
2 I am a file
3 Read me until end-of-file.
Answer:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        int count =0;
        Scanner sc = new Scanner(System.in);
        //String s = sc.readLine();
        
        while(sc.hasNext())
        {
            count ++;
            System.out.println(count+" "+sc.nextLine());
        }
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}

Saturday, October 2, 2021

Grading policy hackerrank

HackerLand University has the following grading policy:

  • Every student receives a  in the inclusive range from  to .
  • Any  less than  is a failing grade.

Sam is a professor at the university and likes to round each student's  according to these rules:

  • If the difference between the  and the next multiple of  is less than , round  up to the next multiple of .
  • If the value of  is less than , no rounding occurs as the result will still be a failing grade.

Examples

  •  round to  (85 - 84 is less than 3)
  •  do not round (result is less than 40)
  •  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, , the number of students.
Each line  of the  subsequent lines contains a single integer, .

Constraints

Sample Input 0

4
73
67
38
33

Sample Output 0

75
67
40
33

Explanation 0

image

  1. Student  received a , and the next multiple of  from  is . Since , the student's grade is rounded to .
  2. Student  received a , and the next multiple of  from  is . Since , the grade will not be modified and the student's final grade is .
  3. Student  received a , and the next multiple of  from  is . Since , the student's grade will be rounded to .
  4. Student  received a grade below , so the grade will not be modified and the student's final grade is .

 


vector<int> gradingStudents(vector<int> grades) {

    vector<int> finalmarks;
    for(int i=0;i<grades.size();i++)
    {
        int chk=grades[i];
        int factor;
        do{
            chk++;
            factor=chk;
        }while(chk%5!=0);
        
        if(grades[i]<38)
        {
            finalmarks.push_back(grades[i]);
        }
        else if(factor-grades[i]<3)
        {
            finalmarks.push_back(factor);
        }
        //if(factor-grades[i]==3)
        else
        {
            finalmarks.push_back(grades[i]);
        }
        
        
    }
    return finalmarks;
}

Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...