-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12180ad
commit 3a4953b
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/codility/p04_counting_elements/PermCheck.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,17 @@ | ||
package codility.p04_counting_elements; | ||
|
||
import java.util.Arrays; | ||
/* | ||
https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/ | ||
*/ | ||
public class PermCheck { | ||
//https://app.codility.com/demo/results/trainingERTVB8-SXV/ | ||
public int solution(int[] a) { | ||
Arrays.sort(a); | ||
int i = 0; | ||
while (i < a.length && a[i] == i + 1) { | ||
i++; | ||
} | ||
return i < a.length ? 0 : 1; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/codility/p04_counting_elements/PermCheckTest.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,28 @@ | ||
package codility.p04_counting_elements; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class PermCheckTest { | ||
PermCheck permCheck; | ||
|
||
@BeforeEach | ||
public void init() { | ||
permCheck = new PermCheck(); | ||
} | ||
|
||
@Test | ||
public void sample1() { | ||
int solution = permCheck.solution(new int[]{4, 1, 3, 2}); | ||
assertThat(solution).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void sample2() { | ||
int solution = permCheck.solution(new int[]{4, 1, 3}); | ||
assertThat(solution).isEqualTo(0); | ||
} | ||
|
||
} |