Resolves Java Priority Queue Task
This commit is contained in:
parent
a54bb59857
commit
fd890ab974
4 changed files with 140 additions and 1 deletions
|
@ -0,0 +1,85 @@
|
||||||
|
package eu.ztsh.training.hackerrank.datastructures.priority_queue;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.PriorityQueue;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
class Priorities {
|
||||||
|
|
||||||
|
List<Student> getStudents(List<String> events) {
|
||||||
|
var queue = new PriorityQueue<>(events.size(),
|
||||||
|
Comparator.comparingDouble(Student::getCGPA)
|
||||||
|
.reversed() // CGPA is the only descending here
|
||||||
|
.thenComparing(Student::getName)
|
||||||
|
.thenComparing(Student::getID));
|
||||||
|
events.forEach(event -> {
|
||||||
|
var line = event.split(" ");
|
||||||
|
switch (line[0]) {
|
||||||
|
case "ENTER" ->
|
||||||
|
queue.add(new Student(Integer.parseInt(line[3]), line[1], Double.parseDouble(line[2])));
|
||||||
|
case "SERVED" -> queue.poll();
|
||||||
|
default -> throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Stream.generate(queue::poll).takeWhile(Objects::nonNull).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Student {
|
||||||
|
|
||||||
|
Student(int id, String name, double cgpa) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.cgpa = cgpa;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getID() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
double getCGPA() {
|
||||||
|
return cgpa;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int id;
|
||||||
|
private final String name;
|
||||||
|
private final double cgpa;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.hackerrank.com/challenges/java-priority-queue/problem
|
||||||
|
public class Solution {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int totalEvents = Integer.parseInt(scan.nextLine());
|
||||||
|
List<String> events = new ArrayList<>();
|
||||||
|
|
||||||
|
while (totalEvents-- != 0) {
|
||||||
|
String event = scan.nextLine();
|
||||||
|
events.add(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Student> students = priorities.getStudents(events);
|
||||||
|
|
||||||
|
if (students.isEmpty()) {
|
||||||
|
System.out.println("EMPTY");
|
||||||
|
} else {
|
||||||
|
for (Student st : students) {
|
||||||
|
System.out.println(st.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static Scanner scan = new Scanner(System.in);
|
||||||
|
private final static Priorities priorities = new Priorities();
|
||||||
|
|
||||||
|
}
|
|
@ -10,6 +10,8 @@ import java.util.List;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public abstract class HackerRankTest {
|
public abstract class HackerRankTest {
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
|
@ -25,6 +27,11 @@ public abstract class HackerRankTest {
|
||||||
System.setIn(originalIn);
|
System.setIn(originalIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void simpleAssert(List<String> input, List<String> expected) {
|
||||||
|
var result = invoke(input);
|
||||||
|
assertThat(result).containsExactlyElementsOf(expected);
|
||||||
|
}
|
||||||
|
|
||||||
protected List<String> invoke(List<String> input) {
|
protected List<String> invoke(List<String> input) {
|
||||||
try {
|
try {
|
||||||
outContent.reset();
|
outContent.reset();
|
||||||
|
|
|
@ -4,7 +4,7 @@ import java.util.Arrays;
|
||||||
|
|
||||||
public record SolutionClassDescription(Class<?> targetClass, String fieldName, FieldModifier[] modifiers) {
|
public record SolutionClassDescription(Class<?> targetClass, String fieldName, FieldModifier[] modifiers) {
|
||||||
|
|
||||||
SolutionClassDescription(Class<?> targetClass) {
|
public SolutionClassDescription(Class<?> targetClass) {
|
||||||
this(targetClass, null, null);
|
this(targetClass, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package eu.ztsh.training.hackerrank.datastructures.priority_queue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import eu.ztsh.training.hackerrank.HackerRankTest;
|
||||||
|
import eu.ztsh.training.hackerrank.SolutionClassDescription;
|
||||||
|
import eu.ztsh.training.hackerrank.SolutionClassDescription.FieldModifier;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@DisplayName("HackerRank chalenges: Java Priority Queue")
|
||||||
|
class SolutionTest extends HackerRankTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCase0() {
|
||||||
|
var input = List.of(
|
||||||
|
"12",
|
||||||
|
"ENTER John 3.75 50",
|
||||||
|
"ENTER Mark 3.8 24",
|
||||||
|
"ENTER Shafaet 3.7 35",
|
||||||
|
"SERVED",
|
||||||
|
"SERVED",
|
||||||
|
"ENTER Samiha 3.85 36",
|
||||||
|
"SERVED",
|
||||||
|
"ENTER Ashley 3.9 42",
|
||||||
|
"ENTER Maria 3.6 46",
|
||||||
|
"ENTER Anik 3.95 49",
|
||||||
|
"ENTER Dan 3.95 50",
|
||||||
|
"SERVED"
|
||||||
|
);
|
||||||
|
var expected = List.of(
|
||||||
|
"Dan",
|
||||||
|
"Ashley",
|
||||||
|
"Shafaet",
|
||||||
|
"Maria"
|
||||||
|
);
|
||||||
|
simpleAssert(input, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SolutionClassDescription getSolutionClassDescription() {
|
||||||
|
return new SolutionClassDescription(Solution.class,
|
||||||
|
"scan",
|
||||||
|
new FieldModifier[]{FieldModifier.PRIVATE, FieldModifier.STATIC});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue