-
Notifications
You must be signed in to change notification settings - Fork 0
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
Pqueue #25
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...hackerrank/advanced/PerformOperation.java → ...rrank/advanced/PerformMathsOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
53 changes: 53 additions & 0 deletions
53
.../java/io/github/clormor/hackerrank/datastructures/priorityqueue/PriorityQueueExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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]))); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/io/github/clormor/hackerrank/datastructures/priorityqueue/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...a/io/github/clormor/hackerrank/datastructures/priorityqueue/TestPriorityQueueExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bugger off