Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pqueue #25

Merged
merged 3 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ plugins {
id 'idea'
id 'maven'
id 'signing'
id 'org.inferred.processors' version '2.2.0'
id 'com.palantir.circle.style' version '1.1.4'
id 'net.researchgate.release' version '2.8.0'
}
Expand All @@ -41,6 +42,8 @@ repositories {
dependencies {
compile "com.google.guava:guava:$guavaVersion"
compile "com.github.spotbugs:spotbugs-annotations:$spotbugsVersion"
implementation "org.immutables:value-annotations:$immutablesVersion"
annotationProcessor "org.immutables:value:$immutablesVersion"
testCompile "junit:junit:$junitVersion"
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/github/clormor/hackerrank/advanced/MyMaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

public class MyMaths {

private PerformOperation isOdd = (int n) -> {
private PerformMathsOperation isOdd = (int n) -> {
return n % 2 != 0;
};

private PerformOperation isPrime = (int n) -> {
private PerformMathsOperation isPrime = (int n) -> {
BigInteger b = new BigInteger(Integer.toString(n));
return b.isProbablePrime(100);
};

private PerformOperation isPalindrome = (int n) -> {
private PerformMathsOperation isPalindrome = (int n) -> {
char[] chars = Integer.toString(n).toCharArray();
for (int i = 0; i < (chars.length / 2); i++) {
if (chars[i] != chars[chars.length - 1 - i]) {
Expand All @@ -23,19 +23,19 @@ public class MyMaths {
return true;
};

PerformOperation isOdd() {
PerformMathsOperation isOdd() {
return isOdd;
}

PerformOperation isPrime() {
PerformMathsOperation isPrime() {
return isPrime;
}

PerformOperation isPalindrome() {
PerformMathsOperation isPalindrome() {
return isPalindrome;
}

boolean checker(PerformOperation op, int n) {
boolean checker(PerformMathsOperation op, int n) {
return op.operation(n);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.github.clormor.hackerrank.advanced;

interface PerformOperation {
interface PerformMathsOperation {
boolean operation(int n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.clormor.hackerrank.datastructures.priorityqueue;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

import static io.github.clormor.hackerrank.datastructures.priorityqueue.Student.COMPARE_STUDENTS;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong lexicographical order for 'io.github.clormor.hackerrank.datastructures.priorityqueue.Student.COMPARE_STUDENTS' import. Should be before 'java.util.PriorityQueue'.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bugger off


public class PriorityQueueExample {
static final String ENTER_COMMAND = "ENTER";
static final String SERVED_COMMAND = "SERVED";

PriorityQueue<Student> students = null;

public List<Student> getStudents(String[] events) {
students = new PriorityQueue<>(events.length, COMPARE_STUDENTS);
for (String event : events) {
processEvent(event);
}
List<Student> result = new ArrayList<>(students.size());
while (!students.isEmpty()) {
result.add(students.remove());
}
return result;
}

private void processEvent(String event) {
String[] parts = event.split(" ");
switch (parts[0]) {
case ENTER_COMMAND:
addStudent(parts);
break;
case SERVED_COMMAND:
removeStudentIfPossible();
break;
default:
throw new RuntimeException("Invalid command " + parts[0]);
}
}

private void removeStudentIfPossible() {
if (! students.isEmpty()) {
students.remove();
}
}

private void addStudent(String[] parts) {
students.add(ImmutableStudent.of(
parts[1],
Double.parseDouble(parts[2]),
Integer.parseInt(parts[3])));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.clormor.hackerrank.datastructures.priorityqueue;

import org.immutables.value.Value;

import java.util.Comparator;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong lexicographical order for 'java.util.Comparator' import. Should be before 'org.immutables.value.Value'.


@Value.Immutable
public abstract class Student {

@Value.Parameter
abstract String getName();

@Value.Parameter
abstract double getCgpa();

@Value.Parameter
abstract int getId();

public static final Comparator<Student> COMPARE_STUDENTS = (Student a, Student b) -> {
if (a.getCgpa() == b.getCgpa()) {
if (a.getName().equals(b.getName())) {
return b.getId() - a.getId();
} else {
return a.getName().compareTo(b.getName());
}
} else {
return (a.getCgpa() > b.getCgpa()) ? -1 : 1;
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void setup() {

@Test
public void test_odd() {
PerformOperation op = m.isOdd();
PerformMathsOperation op = m.isOdd();
assertTrue(m.checker(op, 3));
assertFalse(m.checker(op, 4));
assertFalse(m.checker(op, 0));
Expand All @@ -28,7 +28,7 @@ public void test_odd() {

@Test
public void test_prime() {
PerformOperation op = m.isPrime();
PerformMathsOperation op = m.isPrime();
assertTrue(m.checker(op, 3));
assertFalse(m.checker(op, 4));
assertFalse(m.checker(op, 0));
Expand All @@ -39,7 +39,7 @@ public void test_prime() {

@Test
public void test_palindrome() {
PerformOperation op = m.isPalindrome();
PerformMathsOperation op = m.isPalindrome();
assertTrue(m.checker(op, 3));
assertTrue(m.checker(op, 4));
assertTrue(m.checker(op, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public void test_senior() throws InvocationTargetException, IllegalAccessExcepti

public String test_method(String role, int spend) throws InvocationTargetException, IllegalAccessException {
for (Method m : f.getClass().getDeclaredMethods()) {
// when running tests via gradle, skip classes inserted by jacoco
if (m.getName().contains("jacoco")) {
continue;
}
FamilyBudget budget = getBudget(m);
if (role.equals(budget.userRole())) {
return (String) m.invoke(f, budget.budgetLimit(), spend);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.github.clormor.hackerrank.datastructures.priorityqueue;

import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static io.github.clormor.hackerrank.datastructures.priorityqueue.Student.COMPARE_STUDENTS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TestPriorityQueueExample {

PriorityQueueExample p;

@Before
public void setup() {
p = new PriorityQueueExample();
}

@Test
public void simple_comparisons() {
Student shafaet = ImmutableStudent.of("Shafaet", 3.7, 35);
Student maria = ImmutableStudent.of("Maria", 3.6, 46);
assertTrue(COMPARE_STUDENTS.compare(shafaet, maria) < 0);

Student dan = ImmutableStudent.of("Dan", 3.95, 50);
assertTrue(COMPARE_STUDENTS.compare(dan, shafaet) < 0);

Student secondDan = ImmutableStudent.of("Dan", 3.95, 40);
assertTrue(COMPARE_STUDENTS.compare(dan, secondDan) < 0);

Student asCleverAsDan = ImmutableStudent.of("Zoe", 3.95, 51);
assertTrue(COMPARE_STUDENTS.compare(dan, asCleverAsDan) < 0);
}

@Test
public void hacker_rank_sample_test() {
String[] input = {
PriorityQueueExample.ENTER_COMMAND + " John 3.75 50",
PriorityQueueExample.ENTER_COMMAND + " Mark 3.8 24",
PriorityQueueExample.ENTER_COMMAND + " Shafaet 3.7 35",
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.ENTER_COMMAND + " Samiha 3.85 36",
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.ENTER_COMMAND + " Ashley 3.9 42",
PriorityQueueExample.ENTER_COMMAND + " Maria 3.6 46",
PriorityQueueExample.ENTER_COMMAND + " Anik 3.95 49",
PriorityQueueExample.ENTER_COMMAND + " Dan 3.95 50",
PriorityQueueExample.SERVED_COMMAND
};

List<Student> students = p.getStudents(input);
assertEquals(4, students.size());
checkStudent("Dan", 3.95, 50, students.get(0));
checkStudent("Ashley", 3.9, 42, students.get(1));
checkStudent("Shafaet", 3.7, 35, students.get(2));
checkStudent("Maria", 3.6, 46, students.get(3));
}

@Test
public void its_possible_to_serve_an_empty_queue() {
String[] input = {
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.ENTER_COMMAND + " Samiha 3.85 36",
PriorityQueueExample.ENTER_COMMAND + " Maria 3.6 46",
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.SERVED_COMMAND,
PriorityQueueExample.SERVED_COMMAND
};

List<Student> students = p.getStudents(input);
assertEquals(0, students.size());
}


@Test(expected = RuntimeException.class)
public void throw_runtime_exception_on_bad_command() {
p.getStudents(new String[]{"ioefhewoifhwe"});
}

private void checkStudent(String name, double cgpa, int id, Student student) {
assertEquals(name, student.getName());
assertEquals(cgpa, student.getCgpa(), 0.001);
assertEquals(id, student.getId());
}
}