Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add jUnit test and refactoring for CelebrityProblem.java#53

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
vinhtqfx07044 wants to merge1 commit intorampatra:master
base:master
Choose a base branch
Loading
fromvinhtqfx07044:master
Open
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/main/java/com/rampatra/arrays/CelebrityProblem.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/test/java/CelebrityProblemTest.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp