Hiking around HackerRank — 03

07/15/2020

Note:

  1. All problems are published by HackerRank, credit to HackerRank
  2. The solutions are coded by myself, and open source just for study.

Java Generics

Java Generics, retrieved from https://www.hackerrank.com/challenges/java-generics/problem

Well, I’m not so familiar with generics in java. According to this doc [1], generics would allow the types to be parameters to methods, classes, and interfaces. Sounds similar to the HashSet problem we encountered before, huh?

Let’s give it a try:

import java.io.IOException;
import java.lang.reflect.Method;
class Printer
{
//Write your code here
public void printArray(T[] elements){
for (T element: elements){
System.out.println(element);
}
}
}public class Solution {
public static void main( String args[] ) {
Printer myPrinter = new Printer();
Integer[] intArray = { 1, 2, 3 };
String[] stringArray = {"Hello", "World"};
myPrinter.printArray(intArray);
myPrinter.printArray(stringArray);
int count = 0;
for (Method method : Printer.class.getDeclaredMethods()) {
String name = method.getName();
if(name.equals("printArray"))
count++;
}
if(count > 1)System.out.println("Method overloading is not allowed!"); }
}

Unfortunately, this would generate a compilation error. What’s wrong with it?

Refer to the discussion forum [2], we need T to denote the type, and “Public” is doubtlessly not allowed here. Hmm, let me modify it:

import java.io.IOException;
import java.lang.reflect.Method;
class Printer
{
//Write your code here
<T>void printArray(T[] elements){
for (T element: elements){
System.out.println(element);
}
}
}public class Solution {
public static void main( String args[] ) {
Printer myPrinter = new Printer();
Integer[] intArray = { 1, 2, 3 };
String[] stringArray = {"Hello", "World"};
myPrinter.printArray(intArray);
myPrinter.printArray(stringArray);
int count = 0;
for (Method method : Printer.class.getDeclaredMethods()) {
String name = method.getName();
if(name.equals("printArray"))
count++;
}
if(count > 1)System.out.println("Method overloading is not allowed!"); }
}

It worked! Awesome!

(To be honest, I won’t solve it without the discussion board. That’s some kind of creepy. Sorry about that!)

07/28/2020

Java Sort

(I’ve been occupied by a lot of issues recently, but here I’m back!)

Java Sort, Retrieved from https://www.hackerrank.com/challenges/java-sort/problem

Hmm… The first thing I thought was using a loop. But I noticed the hint: “You can use comparators to sort a list of objects. See the oracle docs to learn about comparators.”

Unfortunately, I’m just a newbie. What is a comparator, and how should I use it?

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, Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getFname).thenComparing(Student::getId)); for(Student st: studentList){
System.out.println(st.getFname());
}
}
}

Wow, it’s unbelievably convenient!

Note: this script requires Java 8.

Extra words:

After stopping practicing for two weeks, I realized how much I had lagged behind others. Coding should be persistent. Keep fighting!

References:

[1] Generics in Java, GeeksforGeeks, retrieved from https://www.geeksforgeeks.org/generics-in-java/

[2] Java Generics, HackerRank, retrieved from hackerrank.com/challenges/java-generics/forum

Undergraduate student / Research assistant/ Always curious / Opinions are mine