Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, World! With C++ - Hacker Rank Solution #include <iostream> #include <cstdio

Java Sort hackerrank solution

 You are given a list of student information: ID, FirstName, and CGPA. Your task is to rearrange them according to their CGPA in decreasing order. If two student have the same CGPA, then arrange them according to their first name in alphabetical order. If those two students also have the same first name, then order them according to their ID. No two students have the same ID.

Hint: You can use comparators to sort a list of objects. See the oracle docs to learn about comparators.

Input Format

The first line of input contains an integer N, representing the total number of students. The next  N  lines contains a list of student information in the following structure:

ID Name CGPA

Constraints:-

  • < N < 1000
  • < ID < 1000
  • < |Name| < 1000
  • < CGPA < 1000
  • The name contains only lowercase English letters. The ID contains only integer numbers without leading zeros. The CGPA will contain, at most, 2 digits after the decimal point.

    Output Format

    After rearranging the students according to the above rules, print the first name of each student on a separate line.

    Sample Input

    5
    33 Rumpa 3.68
    85 Ashis 3.85
    56 Samiha 3.75
    19 Samara 3.75
    22 Fahim 3.76
    

    Sample Output

    Ashis
    Fahim
    Samara
    Samiha
    Rumpa

    Solution:-

    import java.util.*;
    
    class Student{
       private int id;
       private String fname;
       private double cgpa;
       public Student(int id, String fname, double cgpa) {
          super();
          this.id = id;
          this.fname = fname;
          this.cgpa = cgpa;
       }
       public int getId() {
          return id;
       }
       public String getFname() {
          return fname;
       }
       public double getCgpa() {
          return cgpa;
       }
    }
    
    //Complete the code
    public class Solution
    {
       public static void main(String[] args){
          Scanner in = new Scanner(System.in);
          int testCases = Integer.parseInt(in.nextLine());
          
          List<Student> studentList = new ArrayList<Student>();
          while(testCases>0){
             int id = in.nextInt();
             String fname = in.next();
             double cgpa = in.nextDouble();
             
             Student st = new Student(id, fname, cgpa);
             studentList.add(st);
             
             testCases--;
          }
          
          Collections.sort(studentList,new Comparator<Student>(){
            public int compare(Student s1, Student s2){
                if((s1.getCgpa()*100) != (s2.getCgpa()*100)){
                    return (int)((s2.getCgpa()*1000) - (s1.getCgpa()*1000));
                }
                else if(!(s1.getFname().equals(s2.getFname()))){
                    return s1.getFname().compareTo(s2.getFname());
                }
                else{
                    return s1.getId()-s2.getId();
                }
            }
          });    
          
          for(Student st: studentList){
             System.out.println(st.getFname());
          }
       }
    }

    Comments

    Popular Posts