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

add jUnit test and refactoring for CelebrityProblem.java #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 20 additions & 9 deletions src/main/java/com/rampatra/arrays/CelebrityProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class CelebrityProblem {
* @param b
* @return
*/
public static boolean haveAcquaintance(int[][] peoples, int a, int b) {

// Rename Method: haveAcquaintance -> doesKnow, for better clarity
public static boolean doesKnow(int[][] peoples, int a, int b) {
return peoples[a][b] == 1;
}

Expand All @@ -48,36 +50,45 @@ public static boolean haveAcquaintance(int[][] peoples, int a, int b) {
* @param peoples
* @return
*/
public static int findCelebrity(int[][] peoples) {

// Extract Method: Extract the possible celebrity finding logic into a new method
private static Stack<Integer> findPossibleCelebrities(int[][] peoples) {
Stack<Integer> possibleCelebrities = new LinkedStack<>();

for (int i = 0; i < peoples.length; i++) {
for (int j = 0; j < peoples[0].length; j++) {
if (haveAcquaintance(peoples, i, j)) {
if (doesKnow(peoples, i, j)) {
possibleCelebrities.push(j);
}
}
}

return possibleCelebrities;
}
// Extract Method: Extract the celebrity finding logic into a new method
private static int findCelebrityInStack(Stack<Integer> possibleCelebrities, int[][] peoples) {
int firstPerson = -1, secondPerson;

while (!possibleCelebrities.isEmpty()) {
firstPerson = possibleCelebrities.pop();

// we have found the celebrity
if (possibleCelebrities.isEmpty()) break;

if (possibleCelebrities.isEmpty()) {
break;
}
secondPerson = possibleCelebrities.pop();
if (haveAcquaintance(peoples, firstPerson, secondPerson)) {
if (doesKnow(peoples, firstPerson, secondPerson)) {
possibleCelebrities.push(secondPerson);
} else {
possibleCelebrities.push(firstPerson);
}
}

return firstPerson;
}

public static int findCelebrity(int[][] peoples) {
Stack<Integer> possibleCelebrities = findPossibleCelebrities(peoples);
return findCelebrityInStack(possibleCelebrities, peoples);
}

public static void main(String[] args) {
System.out.println(findCelebrity(new int[][]{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 1, 0}}));
System.out.println(findCelebrity(new int[][]{{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}));
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/CelebrityProblemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import com.rampatra.arrays.CelebrityProblem;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CelebrityProblemTest {
@Test
public void testThereNoPerson() {
int[][] peoples = new int[][]{};
Assertions.assertEquals(-1, CelebrityProblem.findCelebrity(peoples));
}
@Test
public void testThereOnlyOnePerson() {
int[][] peoples = new int[][]{{0}};
Assertions.assertEquals(-1, CelebrityProblem.findCelebrity(peoples));
}
@Test
public void testNoCelebrity() {
int[][] peoples = new int[][]{{0, 1, 1, 0},{0, 0, 1, 0},{0, 0, 0, 1},{0, 0, 1, 0}};
Assertions.assertEquals(-1, CelebrityProblem.findCelebrity(peoples));
}
@Test
public void testCelebrityAtPosition0() {
int[][] peoples = new int[][]{{0, 0, 0, 0},{1, 0, 1, 0},{1, 0, 0, 1},{1, 0, 1, 0}};
Assertions.assertEquals(0, CelebrityProblem.findCelebrity(peoples));
}
@Test
public void testCelebrityAtPosition1() {
int[][] peoples = new int[][]{{0, 1, 1, 0},{0, 0, 0, 0},{0, 1, 0, 0},{0, 1, 1, 0}};
Assertions.assertEquals(1, CelebrityProblem.findCelebrity(peoples));
}
@Test
public void testCelebrityAtPosition2() {
int[][] peoples = new int[][]{{0, 1, 1, 0},{0, 0, 1, 0},{0, 0, 0, 0},{0, 1, 1, 0}};
Assertions.assertEquals(2, CelebrityProblem.findCelebrity(peoples));
}
@Test
public void testCelebrityAtPosition3() {
int[][] peoples = new int[][]{{0, 0, 1, 1},{0, 0, 1, 1},{1, 0, 0, 1},{0, 0, 0, 0}};
Assertions.assertEquals(3, CelebrityProblem.findCelebrity(peoples));
}
}